Home > development, Microsoft SharePoint 2010, MS SharePoint Sever, sharepoint, SharePoint 2010 > Create, Add, Delete content type (attach a custom document template)

Create, Add, Delete content type (attach a custom document template)

Creates a custom content type…attaches a custom document template with it…enables content types on document library…..deletes “Document” content type from the library….attaches our custom content type to it…

private void ProcessMyCVContentType(SPWeb web)
                web.AllowUnsafeUpdates = true;
                SPDocumentLibrary cvDocLib = web.Lists.TryGetList(Config.ListName_MyCVs) as SPDocumentLibrary;
                SPContentTypeId myContentTypeId = new SPContentTypeId("0x010100FA0963FA69A648AA916D2E41284FC3D5");
                SPContentType myContentType = web.ContentTypes[myContentTypeId];
                if (myContentType == null)
                    myContentType = new SPContentType(myContentTypeId, web.ContentTypes, "MyCVs");
                    myContentType.Group = "Custom Content Types";
                    myContentType.DocumentTemplate = "/my/_cts/myconenttypetemplate/template.dotx"; //SharedUrlUtility.EnsureTrailingSlash(web.ServerRelativeUrl) + Config.CVTemplatePath; //must be a relative url, no http:// etc in it.....otherwise u get "value does not fall within the expected range"......... 
SPFieldLink fieldLink = new SPFieldLink(cvDocLib.Fields["Primary"]);
                    fieldLink = new SPFieldLink(cvDocLib.Fields["Description"]);
                if (myContentType != null && cvDocLib != null)
                    cvDocLib.ContentTypesEnabled = true;
                    if (cvDocLib.ContentTypes["Document"] != null)
            catch (Exception ex)
                Log.LogError("MyProjName", ex);

make sure to put your custom template (e.g. customtemplate.dotx) at the specified location or a location of your choice (usually _cts folder is used for content types, see in sharepoint designer) and mention a relative url to it…….

Hope this helps-

  1. No comments yet.
  1. July 18, 2012 at 5:57 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 37 other followers

%d bloggers like this: