Adding a Content Type to all Document Libraries

Deadly 提交于 2019-12-13 08:37:57

问题


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?


回答1:


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();
    }
}



回答2:


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.




回答3:


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.




回答4:


The URL field needs to be added to the Schema.XML in the element. The source can be found in one of the source files in the Template directory (can't look it up now). Just copy-paste the element for "URL".



来源:https://stackoverflow.com/questions/2203132/adding-a-content-type-to-all-document-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!