Programmatically Add Excel Word Access Content Types SharePoint 2010

Create new project using visual studio “Empty SharePoint Project” template, and then select sandbox solution, and then add new web part, now we call below function to create new Content Template with particular document and add some site column, then its add one document library.

        private void ContentTypeAdder()
        {
            using (SPSite site = new SPSite(“http://win7”))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPContentType contentType = new SPContentType(web.ContentTypes[“Document”], web.ContentTypes, “SPTECHNET Time Sheet”);
                    web.ContentTypes.Add(contentType);
                    contentType.Group = “SPTECHNET Content Types”;
                    contentType.Description = “Demo content type”;
                    contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Address”)));
                    contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField(“Office”)));
                    contentType.DocumentTemplate = “http://win7/Shared Documents/Test Dummy.docx”;
                    contentType.Update();
                    
                    SPList list = web.Lists[“Shared Documents”];
                    list.ContentTypesEnabled=true;
                    list.ContentTypes.Add(contentType);
                    list.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }

Image

One thought on “Programmatically Add Excel Word Access Content Types SharePoint 2010

  1. Hello Ravi,
    Very good one. But I have small suggestion.
    Your code block has some thing wrong. You are not adding your content type to the site content types. Instead of following line SPContentType contentType = new SPContentType(web.ContentTypes[“Document”], web.ContentTypes, “SPTECHNET Time Sheet”);

    Please correct it to
    SPContentType contentType = web.ContentTypes.Add(new SPContentType(web.ContentTypes[“Document”], web.ContentTypes, “SPTECHNET Time Sheet”));

    Like

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