views:

1273

answers:

3

We have hundreds of document libraries, spread all throughout various site collections in a MOSS 2007 SharePoint site.

The problem is, that I want to add Content Type to show up in addition to the "New Document" and "New Folder" content types: the "Link to a Document" content type (0x01010A). This new content type should should up for all existing and new document libraries.


What I've tried:

I thought that I would be able to add the following to a schema.xml somewhere inside the <ContentTypes></ContentTypes> node:

<!-- Link to Document Content Type -->
<ContentTypeRef ID="0x01010A" />

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\Publishing\Pages\schema.xml

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\DocumentLibrary\DocLib\schema.xml

This seems to have added the content type to the libraries OK, but it doesn't show up under the New menu (still just displays the "Document" and "Folder" content types). If I set "Allow management of content types?" to "Yes" then it shows up.

tl;dr

Q: How do I add the "Link to a Document" content type to all document libraries and have it show up in the New menu?

A: 

If you're comfortable with the SharePoint object model, it's possible to add a content type to a list or library programmatically. So you could write a feature receiver or even just a console application to be run on the server that would iterate through all lists within a site and add the content type.

Your code would look something like:

void AddContentTypeToList(string contentTypeId, SPSite site, SPList list)
{
    SPContentTypeId ctid = new SPContentTypeId(contentTypeId);
    SPContentType ct = site.RootWeb.ContentTypes[ctid];
    if (list.ContentTypes[ct.Name] == null)
    {
        list.ContentTypes.Add(ct);
        list.Update();
    }
}
DylanW
Dylan, thanks for the answer. Unfortunately, this does not satisfy my requirements. The content type does not show up in the "New" menu and the content type won't be included when new Document Libraries are created.
Kit Menke
A: 

To manage buttons in new menu, use SPFolder.UniqueContentTypeOrder property (SPList.RootFolder.UniqueContentTypeOrder will get you there).

To add Content Type to new libraries... hmm... maybe create new List Definition, add a feature that deactivates document library and activates your custom document library feature? Probably too much effort if you have to modify existing, production SharePoint web's.

By the way it is not wise to modify built - in features (update's can overwrite them). There is a ContentTypeBinding feature element for this purpose. However it just binds ContentType to 1 list.

Janis Veinbergs
Thanks. I was wondering how I would be able to add the content type to the new button. I am really hoping I won't have to write a console app to manage content types on all libraries (will get really ugly). I will need to investigate ContentTypeBinding.
Kit Menke
What do you exactly mean by "Add content type to new button"?Does this screenshot express your need: http://img40.imageshack.us/img40/8552/ss20100208104600.png ?If so, that's what UniqueContentTypeOrder property is for.
Janis Veinbergs
Sorry... wasn't very clear. The code in your answer looks like it should work for adding a content type to the "New" button. :)
Kit Menke
A: 

After doing some more investigation, I found out that by changing ..\12\TEMPLATE\FEATURES\DocumentLibrary\DocLib\schema.xml I could add the Link to a Document content type to all NEW document libraries. Changing the schema.xml and then doing an IISRESET did not change existing libraries.

In order to do this, the beginning of my schema.xml looked something like this:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="$Resources:shareddocuments_Title;" Direction="$Resources:Direction;" Url="Shared Documents" BaseType="1" EnableContentTypes="TRUE">
  <!-- Link to Document Content Type - Added EnableContentTypes="TRUE" -->
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x0101">
        <Folder TargetName="Forms/Document" />
      </ContentTypeRef>
      <ContentTypeRef ID="0x0120" />
      <!-- Link to Document Content Type -->
      <ContentTypeRef ID="0x01010A" />
    </ContentTypes>

Since this didn't change existing document libraries, I will need to write a console application that uses the code specified in the other two answers to update each library.

Kit Menke