Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've created several content type that inherit from the document content type in the site content type lists. Please refer to the image below MyContentType

How do I get these these content type pro-grammatically. Basically what I wants to do is to show these content type in a drop down lists so that the user can select which document library to upload a file to. I'm stuck at there

 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPContentType documentContentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];
            }
        }

Please provide some guidance. Thanks

share|improve this question
    
Thanks for the answer guys, Is there any way for me to get my custom content types based on the custom group I have created. In my case the group name is SampleDocContentType. If I get my content type based on the base id, Document. Then it will get all the ContentType that inherit this ContentType even the content types that I don't want them to appear in my dropdownlist. –  Agamand The True Jan 30 '12 at 2:15
add comment

4 Answers 4

up vote 8 down vote accepted

I thik that you should create a base content type which is inherited from Document content type. Then you should inherit Arvhitecture Document, Manual Document, etc. content types from your base content type.

After this you can select your content types by base content type like this:

SPContentTypeId id = new SPContentTypeId(BaseContentTypeId);
IList<SPContentType> ContentTypes = web.ContentTypes.Cast<SPContentType>().Where(c => c.Id.IsChildOf(id)).ToList();
share|improve this answer
add comment

You could accomplish this using the SPContentType.Parent property.

Your sample code might look like this. I have not executed this code and checked, so modify wherever required.

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
  using (SPWeb web = site.OpenWeb())
   {
     SPContentTypeCollection allContentTypes = web.ContentTypes;
                SPContentTypeCollection docContentTypes = null;
                foreach (SPContentType objContentType in allContentTypes)
                {
                    if (objContentType.Parent.Name == "Document")
                    {
                        docContentTypes.Add(objContentType);
                    }
                }
                // docContentTypes contains all content types derived from content type "Document". You can implement additional checks and add this into a drop down list.
    }
}
share|improve this answer
1  
if there are several 3rd party solutions in the site which install own lists that are inherited from document type, it can break an logic of our solution. –  Alexander Jan 28 '12 at 6:41
add comment

Unfortunately the above code won't work - this is because you can't reference the SPContentTypeCollection if null (and can't initialize it using 'new...' either). Use a list to do it:

using System.Collections.Generic;
using Microsoft.SharePoint;
...
using (SPSite GetSite = new SPSite("http://site"))
{
    using (SPWeb TopWeb = GetSite.OpenWeb())
    {
            SPContentTypeCollection CurContentTypes = TopWeb.ContentTypes;
            List<SPContentType> SelectedTypes = new List<SPContentType>();
            foreach (SPContentType SPCT in CurContentTypes)
            {
                if (SPCT.Group == "MyGroup")
                {
                    SelectedTypes.Add(SPCT);
                }
            }
    }
}

Once this is done, you can simply iterate through the list

share|improve this answer
add comment

Why don't you use the Group property of SPContentType class:

using (SPSite siteCollection = new SPSite("put your site url here"))
        {

            using (SPWeb Web = siteCollection.OpenWeb())
            {

                SPContentTypeCollection SPCTCol = Web.ContentTypes;
                List<string> CustomTypesList = new List<string>();
                foreach (SPContentType ContentType in SPCTCol)
                {
                    if (ContentType.Group == "put your content type group name here")
                    {
                        CustomTypesList.Add(Convert.ToString(ContentType.Name));
                    }
                }
            }
        }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.