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;
}
}
}
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”));
LikeLike