Hi Friend,
Here is sample code to add / delete content type in list / library programmatically.Also i will create the site column and add into custom content type.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spCurrentSite = properties.Feature.Parent as SPSite)//new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spCurrentweb = spCurrentSite.OpenWeb())
{
//Delete existing content type
spCurrentweb.AllowUnsafeUpdates = true;
SPContentType newwvOCSDoc = spCurrentweb.ContentTypes.Cast<SPContentType>().FirstOrDefault(c => c.Name == "wvOCSDoc");
if (newwvOCSDoc != null)
{
newwvOCSDoc.Delete();
}
//Delete existing site columns
SPField CountryCode= spCurrentweb.Fields.Cast<SPField>().FirstOrDefault(f => f.StaticName == "wvCountryCode");
if (CountryCode!= null)
{
CountryCode.Delete();
}
SPField ProjectID = spCurrentweb.Fields.Cast<SPField>().FirstOrDefault(f => f.StaticName == "wvProjectID");
if (ProjectID != null)
{
ProjectID.Delete();
}
//Now add new content type in site
SPContentType myContentType = new SPContentType(spCurrentweb.ContentTypes["Document"], spCurrentweb.ContentTypes, "wvOCSDoc");
myContentType.Group = "Custom Content Types"; //Group name
//Create new Site Columns
spCurrentweb.Fields.Add("wvCountryCode", SPFieldType.Text, false);
spCurrentweb.Fields.Add("wvProjectID", SPFieldType.Text, false);
SPFieldLink Author = new SPFieldLink(spCurrentweb.Fields["Author"]);
myContentType.FieldLinks.Add(Author);
SPFieldLink wvCountryCode = new SPFieldLink(spCurrentweb.Fields["wvCountryCode"]);
myContentType.FieldLinks.Add(wvCountryCode);
SPFieldLink wvProjectID = new SPFieldLink(spCurrentweb.Fields["wvProjectID"]);
myContentType.FieldLinks.Add(wvProjectID);
spCurrentweb.ContentTypes.Add(myContentType);
myContentType.Update();
spCurrentweb.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{ }
}
Now you can see the "wvOCSDoc" content type in site content type section of your site.
Here is sample code to add / delete content type in list / library programmatically.Also i will create the site column and add into custom content type.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spCurrentSite = properties.Feature.Parent as SPSite)//new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spCurrentweb = spCurrentSite.OpenWeb())
{
//Delete existing content type
spCurrentweb.AllowUnsafeUpdates = true;
SPContentType newwvOCSDoc = spCurrentweb.ContentTypes.Cast<SPContentType>().FirstOrDefault(c => c.Name == "wvOCSDoc");
if (newwvOCSDoc != null)
{
newwvOCSDoc.Delete();
}
//Delete existing site columns
SPField CountryCode= spCurrentweb.Fields.Cast<SPField>().FirstOrDefault(f => f.StaticName == "wvCountryCode");
if (CountryCode!= null)
{
CountryCode.Delete();
}
SPField ProjectID = spCurrentweb.Fields.Cast<SPField>().FirstOrDefault(f => f.StaticName == "wvProjectID");
if (ProjectID != null)
{
ProjectID.Delete();
}
//Now add new content type in site
SPContentType myContentType = new SPContentType(spCurrentweb.ContentTypes["Document"], spCurrentweb.ContentTypes, "wvOCSDoc");
myContentType.Group = "Custom Content Types"; //Group name
//Create new Site Columns
spCurrentweb.Fields.Add("wvCountryCode", SPFieldType.Text, false);
spCurrentweb.Fields.Add("wvProjectID", SPFieldType.Text, false);
SPFieldLink Author = new SPFieldLink(spCurrentweb.Fields["Author"]);
myContentType.FieldLinks.Add(Author);
SPFieldLink wvCountryCode = new SPFieldLink(spCurrentweb.Fields["wvCountryCode"]);
myContentType.FieldLinks.Add(wvCountryCode);
SPFieldLink wvProjectID = new SPFieldLink(spCurrentweb.Fields["wvProjectID"]);
myContentType.FieldLinks.Add(wvProjectID);
spCurrentweb.ContentTypes.Add(myContentType);
myContentType.Update();
spCurrentweb.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{ }
}
Now you can see the "wvOCSDoc" content type in site content type section of your site.
Hi Hemendra
ReplyDeleteCongratulations..!, At the last you made your blog. Hoping for good post now.
This is how to add a content type to an SPWeb, not a library or a list as your title suggests.
ReplyDeleteTitle is misleading
ReplyDelete