Steps to create a Content type using Client object model are
1. To create a content type using the client object model in Visual Studio 2010, create a new project (File ➪ New ➪ Project) and select a Console project template.
2. After the project starts, you will add references to the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll assemblies located in the 14\ISAPI folder also listed under the .NET component listing in the Add References dialog box.
3. Next, write the code below in you main method. Please note that unlike, server object model, client object model makes use of an Information class that contains the template of the content type. The information class works in a similar manner to the SPContentType class with the exception of the parent ’ s reference; rather than pass in a reference to the parent ’ s content type, client object model code gets the parent ’ s ID.
static void Main(string[] args)
{
// Get a reference to the site collection
ClientContext clientContext = new ClientContext(“http://SPsite”);
Web web = clientContext.Web;
// Load reference to content type collection
ContentTypeCollection contentTypes = web.ContentTypes;
clientContext.Load(contentTypes);
clientContext.ExecuteQuery();
// Create a Content Type Information object
ContentTypeCreationInformation _customcontenttype = new ContentTypeCreationInformation();
customcontenttype.Name = “Custom Content type”;
customcontenttype.ParentContentType = contentTypes.GetById(“ADD PARENT CONTENT TYPE ID”);
customcontenttype.Group = “Custom content type group”;
// Create the content type
ContentType myContentType= contentTypes.Add(customcontenttype);
clientContext.ExecuteQuery();
}
4. After the Information object is completed, a ContentType object is added to the ContentTypes collection that you loaded with the ClientContext using the Information object.
No comments:
Post a Comment