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)
        {
            try
            {
                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");
                    web.ContentTypes.Add(myContentType);
                    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"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    fieldLink = new SPFieldLink(cvDocLib.Fields["Description"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    myContentType.Update();
                }                            
                                            
                if (myContentType != null && cvDocLib != null)
                {
                    cvDocLib.ContentTypesEnabled = true;
                    if (cvDocLib.ContentTypes["Document"] != null)
                        cvDocLib.ContentTypes.Delete(cvDocLib.ContentTypes["Document"].Id);
                    cvDocLib.ContentTypes.Add(myContentType);
                    cvDocLib.Update();
                }                       
            }
            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-
Rehman.

About these ads
  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

Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: