0

我正在开发一项将提供给 SharePoint 2007 Web 的功能。功能配置文件如下。

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

  1. 要在激活该功能的 Web 下创建一个名为 xxx 的列表。
  2. 要在该列表下创建一个名为 yyy 的文件夹。
  3. 要放置在该文件夹下的文件 page1.aspx。

现在我在尝试激活该功能时遇到错误,但如果我手动创建列表和文件夹,那么文件就会放在那里。

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

特征.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>

元素.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>
4

1 回答 1

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();
        }
    }
}
于 2013-10-24T12:59:24.087 回答