Products and Services
Graffiti CMS
Learn more about our simple content publishing platform.
Harvest Reporting Server
Get business intelligence tools for measuring online behavior.
Professional Services
Consulting, creative, and Web services from the people who know Community Server best.
Solutions
Telligent
Learn more about our team at Telligent.com.
 

Common Code Examples

Below are examples of very basic code that you can use to accomplish common tasks with the Community Server API. To begin you should make sure that you have a project set up with references to the Community Server assemblies.

The using statements that you will need for this to compile are:

using System;
using CommunityServer.Components;

Create New User

void CreateNewUser()
{
    string status = null;
    User user = new User("testUserName");
    user.Email =
email@email.com;
    user.Password = "somepassword";
    // Provide the User object and a boolean if you want to send them an email regarding their new account
    switch (CommunityServer.Users.Create(user, false))
    {
        case CreateUserStatus.Created:
            status = "Everything worked correctly and the user is created";
            break;
       case CreateUserStatus.DisallowedUsername:
            status = "The Username is not allowed";
            break;
       case CreateUserStatus.DuplicateUsername:
            status = "The username is already in use";
            break;
    }
}

Create Group

void CreateGroup()
{
    Group group = new Group("My Group Name");
    group.Description = "You can specific a description for the group";
    // You should also specify what application type the group belongs to
    group.ApplicationType = ApplicationType.Weblog;

    int groupId = Groups.AddGroup(group);
}

Create Section

void CreateSection()
{
    // A Weblog, Folder, Forum, and Gallery are all types of Sections
    CommunityServer.Blogs.Components.Weblog weblogSection = new CommunityServer.Blogs.Components.Weblog();
    // Set some standard properties
    weblogSection.Name = "test";
    weblogSection.ApplicationType = ApplicationType.Weblog;
    weblogSection.IsActive = true;
    weblogSection.ApplicationKey = "AppKey_For_Weblog";
    int weblogSectionId = Sections.AddSection(weblogSection);
}

Create Thread

void CreateThread()
{
    // A Thread is a type of ForumPost
    CommunityServer.Discussions.Components.Thread thread = new CommunityServer.Discussions.Components.Thread();
    thread.Name = "My new thread";
    thread.IsApproved = true;
    // You would want to also get the current user and add them to the post as they are the post author
    thread.UserID = CSContext.Current.User.UserID;
    // It is also a good idea to assign a forum section to a thread
    CommunityServer.Discussions.Components.ForumPost post = CommunityServer.Discussions.Components.Posts.AddPost(thread);
}