在SharePoint 2007中激活功能时自动创建列表和文件夹

时间:2013-10-23 17:50:50

标签: sharepoint sharepoint-2007 moss wsp sharepoint-feature

我正在开发一个将配置到SharePoint 2007网站的功能。功能配置文件如下。

安装并激活该功能时我想要发生的事情:

  1. 要在要素所在的网站下创建的名为xxx的列表 激活。
  2. 要在该列表下创建名为yyy的文件夹。
  3. 要放在该文件夹下的文件page1.aspx。
  4. 现在我在尝试激活该功能时遇到错误,但如果我手动创建列表和文件夹,那么该文件就会放在那里。

    所以问题是,如何确保自动创建列表和文件夹

    feature.xml的

    <?xml version="1.0" encoding="utf-8"?>
    <Feature  Id="5EAAAAD9-E885-43f8-B2FD-4C63271E7BAA"
              Title="ABC"
              Description="ABC"
              Version="1.0.0.0"
              Hidden="FALSE"
              Scope="Web"
              xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="elements.xml"/>
    
        <ElementFile Location="CustomPages/yyy/page1.aspx" />
      </ElementManifests>
    </Feature>
    

    Elements.xml的

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    
      <Module Name="Module1" Url="xxx/yyy" RootWebOnly="TRUE" Path="CustomPages/yyy">
        <File IgnoreIfAlreadyExists="TRUE" Type="GhostableInLibrary" Url="page1.aspx"></File>
      </Module>
    
    </Elements>
    

1 个答案:

答案 0 :(得分:2)

如果您可以使用自定义代码,则可以覆盖该功能的FeatureActivated event receiver以创建列表和文件夹。

您需要创建另一个功能,以便在配置文件的功能之前运行,并使文件提供功能依赖于第一个功能,以确保列表可用。

或者,您可以通过xml添加列表定义和实例。我个人尽可能避免使用此路线,但您可以在MSDN - Creating List Definitions with Custom List Columns for SharePoint Server 2007

找到相关指导

通过代码添加列表的示例(请注意 - 我没有2007进行测试,但这确实适用于2010,我认为我没有使用任何2010特定代码)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    SPWeb web = properties.Feature.Parent as SPWeb; // Assuming web scoped feature
    SPList customPagesList;
    bool listExists = true;
    //Check to see if the list exists, this method sucks but 2007 doesn't have web.TryGetList()
    try
    {
        customPagesList = web.GetList("/CustomPages"); // server relative url of the list
    }
    catch (FileNotFoundException e)
    {
        listExists = false;
    }

    if (!listExists)
    {
        // Create list and record returned guid
        Guid customPagesListGuid = web.Lists.Add("CustomPages",
            "Library to store web pages used in the site", SPListTemplateType.DocumentLibrary);
        //Get list from stored guid
        customPagesList = web.Lists[customPagesListGuid];
        // Set list properties and add required content types
        customPagesList.Title = "CustomPages";
        customPagesList.OnQuickLaunch = false; // Set to true to display on the quick launch
        customPagesList.ContentTypesEnabled = true;
        customPagesList.NoCrawl = true; // Set to false if you want pages indexed by search
        customPagesList.EnableFolderCreation = true;
        customPagesList.EnableSyndication = false; // Turn off rss
        SPContentType webPartPageCT = web.AvailableContentTypes[SPBuiltInContentTypeId.WebPartPage];
        SPContentType basicPageCT = web.AvailableContentTypes[SPBuiltInContentTypeId.BasicPage];
        customPagesList.ContentTypes.Add(webPartPageCT);
        customPagesList.ContentTypes.Add(basicPageCT);
        // Remove the default content type added on list creation if it is not needed
        DeleteContentType(customPagesList.ContentTypes, "Document");

        // Commit changes                   
        customPagesList.Update();

        //Get library from stored guid
        SPDocumentLibrary customPagesLibrary = (SPDocumentLibrary)web.Lists[customPagesListGuid];
        customPagesLibrary.Folders.Add("/Lists/CustomPages/yyy", SPFileSystemObjectType.Folder);
        string rootFolderUrl = customPagesLibrary.RootFolder.ServerRelativeUrl;
        SPListItem newFolder = customPagesLibrary.Folders.Add(rootFolderUrl, SPFileSystemObjectType.Folder, "yyy");
    newFolder.Update();
    }

}

private void DeleteContentType(SPContentTypeCollection ctCollection, string ctName)
{
    foreach (SPContentType ct in ctCollection)
    {
        if (ct.Name.Equals(ctName))
        {
            ct.Delete();
        }
    }
}