Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘server object model’

Create Content Type using Object Model

Posted by Paul on October 30, 2014

In this article we can try creating a Content Type using Server Object Model code.

Steps

Please follow the steps below to create the content type.

Create a Console Application & Add Reference to Microsoft.SharePoint.dll. Also make sure the project properties > Build tab > Prefer 32-bit is unchecked.

image

Use the following code for the console application.

using (SPSite site = new SPSite(“http://hpvm”))

{

using (SPWeb web = site.OpenWeb())

{

// Delete content type if already existss

if (web.ContentTypes[“CodeCT”] != null)

web.ContentTypes[“CodeCT”].Delete();

// Create content type inheritin from ‘Item’

SPContentType contentType = new SPContentType(web.ContentTypes[“Item”], web.ContentTypes, “CodeCT”);

web.ContentTypes.Add(contentType);

contentType.Group = “Custom Content Types”;

contentType.Description = “Content Type created through Code”;

// Create Site Columns

string fieldName = web.Fields.Add(“Column 1”, SPFieldType.Text, false);

SPField field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 2”, SPFieldType.Number, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

fieldName = web.Fields.Add(“Column 3”, SPFieldType.DateTime, false);

field = web.Fields.GetFieldByInternalName(fieldName);

field.Update();

// Add Field References to Our Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 1”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 2”)));

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Column 3”)));

// Add few fields from OOB Site Columns

contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“_Status”)));

// Update

contentType.Update();

}

}

Console.WriteLine(“Content Type created successfully.”);

}

The code is executed below.

1. Delete any content type with name CodeCT

2. Create new content type

3. Add custom field definitions to the web

4. Add custom field references to the content type

5. Add OOB column _Status to the content type

6. Updates content type

image

For custom columns, we are actually creating a Site Column through code (Fields.Add) & Referring it to the content type (FieldLinks.Add).

Execute the code.

image

You can verify the new content type being created or not from Site Settings > Site content types.

image

Click on it & ensure the all columns are visible.

image

You can use the SPFieldType enum for finding more types.

References

http://msdn.microsoft.com/en-us/library/ff798370.aspx

Summary

In this article we have explored how to create content type using server object model code. The source code is attached for reference.

Advertisements

Posted in SharePoint, SharePoint 2013 | Tagged: , , , | Leave a Comment »

Programmatically Read Discussion Board & Replies

Posted by Paul on October 21, 2014

I searched a lot for Server Object Code in reading Discussion Board Items & Replies. As cannot find one, following is created.

Challenge

You need to programmatically get all discussion items along with their replies.

Steps

This is our Discussion Board.

image

This is the code to retrieve all discussion items.

using (SPSite site = new SPSite(“http://hpvm”))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists.TryGetList(“Discussion Board”);

if (list != null)

{

SPQuery query = new SPQuery();

query.ViewFields = “<FieldRef Name=’Title’ />”;

SPListItemCollection itemColl = list.GetItems(query);

foreach (SPListItem item in itemColl)

{

System.Console.WriteLine(item[“Title”].ToString());

}

}

}

}

Make sure the code is returning values like this.

image

Now we are writing the code to return the replies.

private static void GetReplies(SPList list, SPListItem item)

{

SPQuery query = new SPQuery();

query.Folder = item.Folder;

SPListItemCollection collection = list.GetItems(query);

SPListItem replyItem = collection[0];

string body = (string)replyItem[“Body”];

string author = (string)replyItem[“Author”];

System.Console.WriteLine(body);

System.Console.WriteLine(author);

}

Following is the output on running the console application.

image

You can see the Author is returned mixing with ID. You can use the following method to extract the correct User Name from the string.

private static string GetUserName(string author, SPList list)

{

string result = author;

if (author.Contains(“;”))

{

int id = int.Parse(author.Split(‘;’)[0]);

SPUser user = list.ParentWeb.SiteUsers.GetByID(id);

result = user.Name;

}

return result;

}

Now the user name seems to be better.

image

image

I am creating a Console Application to retrieve the same.

References

http://bit.ly/1tIX5NV

Summary

In this article we have explored how to fetch Discussion Board Items & Replies. The source code is attached here with the article.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »