2013-10-23 78 views
0

我正在开发一个功能,将配置到SharePoint 2007网站。功能配置文件如下。在SharePoint 2007中激活功能时自动创建列表和文件夹

我想什么时候安装并激活该功能的情况发生:

  1. 清单名为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> 

回答

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

我真的必须为此创建另一个功能?感觉像一个矫枉过正的...我希望有更好的办法。 :) – rickythefox

+0

如果您使用代码来调配aspx文件,或者如果使用xml调配库和它的实例,则可以使用一个功能。这里有两个功能的原因是确保在文件配置之前创建库。只是要清楚,如果您确实使用了两个功能,它们都将包含在一个wsp包中,所以仍然只有一个部署,我认为如果您设置了激活依赖关系并将父功能标记为隐藏,那么当子功能被激活,它会自动激活父功能。 –

+0

太好了,谢谢! – rickythefox

相关问题