【问题标题】:Automatically creating a list and a folder when activating a feature in SharePoint 2007在 SharePoint 2007 中激活功能时自动创建列表和文件夹
【发布时间】:2013-11-02 03:41:05
【问题描述】:

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

我希望在安装和激活该功能时发生什么:

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

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

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

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>

【问题讨论】:

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


    【解决方案1】:

    如果您可以使用自定义代码,则可以覆盖该功能的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();
            }
        }
    }
    

    【讨论】:

    • 我真的必须为此创建另一个功能吗?感觉有点矫枉过正......我希望有更好的方法。 :)
    • 如果您使用代码配置 aspx 文件或使用 xml 配置库及其实例,则可以使用一项功能。这里有两个功能的原因是确保在配置文件之前创建库。只是要清楚,如果您确实使用了两个功能,它们都将包含在一个 wsp 包中,因此仍然只有一个部署,而且我认为如果您设置激活依赖项并将父功能标记为隐藏,那么当子功能被激活它会自动激活父功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多