views:

98

answers:

1

In MOSS2007 I need to find and remove for example the default content type from a list (in order to be replaced by a custom one). The list can be on multiple sites, the sites can be in multiple languages and the name for this content type can be diffrent (ex: "Wiki Page" in EN, "Wikipagina" in NL etc.) My ideea was to find the content type using the Id or the prefix of Id (for ex: wiki page start always with 0x010108). Is there any better ideea ? Can we get in code the name of contentypes depending of the language ?

private static SPContentType GetWikiPageContentTypeFromList(SPList list)
    {
        string wikiPageStartId = "0x010108";
        foreach (SPContentType contentType in list.ContentTypes)
        {
            string ctId = contentType.Id.ToString();
            if (ctId.StartsWith(wikiPageStartId))
            {
                return contentType;
            }
        }
        return null;
    }
+1  A: 

You can use SPBuiltInContentTypeId class to get built in content type id's. So why use names if you can use id's which is much more better?

Localized strings

Ofcourse, you may also use names, but then you should use SPUtility.GetLocalizedString. Examine C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\Resources\core.resx to see what resource names have which values.

string strWikiDocumentTitle = SPUtility.GetLocalizedString("$Resources:WikiDocument", "core", SPContext.Current.Web.Language);

Content type id's and hierarchy

Speaking about id's, content types have their own hierarchy and you're right that content types like wiki and all content types derived from wiki will start with 0x010108.

Anyway, you're on the right path.

//Returns best match, that is the content type that is Wiki document. If wiki document content type not in list, will return it's parent content type id.
SPContentTypeId bestMatch = list.ContentTypes.BestMatch(SPBuiltInContentTypeId.WikiDocument);
if (bestMatch.IsChildOf(SPBuiltInContentTypeId.WikiDocument))
{
   return list.ContentTypes[bestMatch];
}

A catch about list content type's

By the way, content type that is returned will not have exactly ID 0x010108, but rather 0x010108xxxxxxxxxxxxxxxxx.... (it will be CHILD if wiki content type), because as you add content type to list it actually creates new content type that is derived from it's parent.

So you can safely delete that content type if you wish. And if you want to modify that content type, then use returned content type's PARENT (SPContentType.Parent property) to modify and apply changes to all content types inherited...

Janis Veinbergs