Online Training On SharePoint
                      
Showing posts with label Hiding Content Type in SharePoint. Show all posts
Showing posts with label Hiding Content Type in SharePoint. Show all posts

Tuesday, 1 November 2011

Disabling A Content Type to be used in List or Libraries in SharePoint

We have a Content Type that was getting used for a Document Library. This content type has some special settings and it was required that no other document library can again use this Content Type. We can do it using OOB settings with the following steps:
1. Create a Content Type by using general settings. Since we were using it inside a document library we have inherited it from the Document Content Type:


2. Associate this Content Type with the document library with which you want to associate it.
3. Now go to Content Type Settings.
4. Now in this step we will use the trick. Here we will change the group of the Content Type. We will create a new group with the name '_Hidden'. Make sure to use the same with the capital and small letters:


5. Once we do this, this Content Type will not be available to be used in other document library. Also this group will also not be visible from the Content Type Gallery. So if at later stage you want to modify the setting of this Content Type you can not do it from here. To modify settings for this Content Type in future remember to copy the URL of the setting page for this Content Type and note it down!!!

Tuesday, 3 March 2009

Hiding/Displaying Content Type with SharePoint Object Model

I had a document library with which multiple content types are associated. When a user click the New button in the document library, I need to show or hide these content types based on the number of documents associated with a particular content type.When a specified number of documents are uploaded for a specific content type user should not be able to see this content type under the new button and when some documents associated to this content type, are deleted the content type should start appearing under the New menu item.

We can achieve this with the help of SharePoint Object Model. First we will find the number of documents for any specific content type in the document library and then in ItemUpdated event and ItemDeleted Events for the document library we can have the code to hide/display the Content Type:

To calculate the number of items related to a specific Content Type in a document library we can use following Code (cant put the code so putting it in sky drive):

private int FindNumberOfItems(SPList list)
{
SPQuery qry = new SPQuery();
qry.Query = "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>ContentTypeName</Value></Eq></Where>";
return list.GetItems(qry).GetDataTable().rows.count;
}

Download the code from:


So lets say X is the Number of Docs associated to a particular content type returned by the above code and now we want to hide the content Type when this number reaches 5 then the code required to be used is:

Code in the ItemUpdated Event:
If (X>=5)
properties.List.ContentTypes["NameOfContentType"].Hidden = true;
//This will make the Content Type not visible under “New” button in the toolbar
properties.List.ContentTypes["NameOfContentType"].Update();

Now same way we can start displaying this content type once the number of docs reaches below the specified limit. We need to call the code in the ItemDeleted Event.

Code in the ItemDeleted Event:
If (X<5) properties.List.ContentTypes["NameOfContentType"].Hidden = false; //This will make the Content Type visible under “New” button in the toolbar
properties.List.ContentTypes["NameOfContentType"].Update();

You may be interested to see
Introduction to Content Types

Related Posts with Thumbnails