Hi, I have created MIS library in sharePoint site and allowed manage content type.
I added two content types in MIS library names are “MIS Monthly” and “MIS Weekly” and one more content type given by default name is “Documents”.
Now I want to display the names of content types and columns also added in these content types by programmatically in c#.
And I wrote below code:
------------------------------------------------------------------------------------------------------------
mySite = SPContext.Current.Site;
myWeb = mySite.OpenWeb();
list = myWeb.Lists["MIS"]; //Library Name.
foreach (SPContentType ct in list.RootFolder.UniqueContentTypeOrder)
{
ListBox1.Items.Add(ct.Name); // Content types will come in list box.
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
mySite = SPContext.Current.Site;
myWeb = mySite.OpenWeb();
SPContentType ctt = myWeb.ContentTypes[ListBox1.Text];
foreach (SPField field in ctt.Fields)
{
if (field.Title != "Content Type" && field.Title != "Document Modified By" && field.Title != "Document Created By")
{
if (field.Hidden == false)
{
ListBox2.Items.Add(field.Title); /*fields(Columns) will come in another list box those who are added in content type at site level.*/
}
}
}
}
------------------------------------------------------------------------------------------------------------
I have created Content types on Site level and added site columns into these content types and add content types in Document library by this option ¿ Add from existing site content types.
Now what happening, above code is working fine and showing content type names which are added in library and clicking on any of those content type showing columns respectively.
PROBLEM--->But IF I am going to hide or remove columns from content types from library settings still those columns are coming in results (when code is running and it should not come), but if those columns are hide or removed from content type from Site level setting then not coming in result, and if I will remove or hide from site level setting in content type then it will affect other libraries because the same content types are used in other libraries also and having same columns with different Status (means- Required, Optional, Hidden).
So what should I change in code?
Waiting for reply.
You have to differentiate between two types of Columns
[1] Site Columns
[2] Library Columns
when you add columns to your site , they are only visibile to your library or list.
If you are working with Content types, they are at the Site collection level , and they are only controlled at that level ,
If you plan to show or hide a Columun you have to do that at the Content type level not the Library level and that will show directly at the List / Library level.
To fix your issue , if you plan to use a specific column in your library that is not being used anywhere else in the site / web , create that only at the Library level and you can hide it as well.
Best of Luck.