如何从Sharepoint 2010功能事件接收器中的XML文件中检索数据?

时间:2011-04-06 18:12:39

标签: sharepoint sharepoint-2010

我正在关注 this tutorial ,我正在尝试在事件接收器中设置代码。

我需要2个属性将SPWeb和字符串发送到他们的方法中。

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // is there a way to make this non hardcoded? 
    SPSite site = new SPSite("http://localhost.com");
    SPWeb web = site.OpenWeb("/");
    string XMlPath = // get xml file path
    CreateGroups(web, path);
}

private void CreateGroups(SPWeb currentSite, string groupsFilename)
{

}

所以我尝试使用getFullPath,但这不起作用。我也尝试使用MapPath,但我似乎无法访问它。

那么如何获取XML文件(我认为这就是我需要的)?

1 个答案:

答案 0 :(得分:5)

  1. 您需要处置SPSite / SPWeb对象,这通常在using子句中完成。
  2. 您不需要在功能接收器中使用绝对路径(硬代码),因为该功能已经是网站/网站范围
  3. 您的XmlPath通常需要指向Sharepoint服务器上的文件,该文件也部署在您的功能中 - 因为功能接收器正在运行 所有正常文件已部署,你很好。
  4. 不用多说,代码略有不同:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        //Web scoped feature?
        //spWeb = (SPWeb) properties.Feature.Parent;
        //assuming Site scoped feature
        spWeb = ((SPSite) properties.Feature.Parent).RootWeb;
    
        using (spWeb)
        {
            string XmlPath = properties.Definition.RootDirectory + @"\Xmlfile\groups.xml"
            CreateGroups(spWeb, XmlPath);
        }
    }
    

    那么如何将XML文件导入“\ Xmlfile \ groups.xml”?只需创建一个模块! (添加新项目>模块) 模块的elements.xml应如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <Module Name="Xmlfile" Path="Xmlfile">
            <File Path="groups.xml" Url="Xmlfile/groups.xml" />
        </Module>
    </Elements>
    

    当然,您需要将groups.xml文件添加到该模块中(上下文菜单&gt;添加现有项目)才能生效。
    另请注意,您可以轻松调试功能接收器,只需确保将部署配置设置为“无激活”(项目属性&gt; Sharepoint&gt; Active Deployment Configuration) - 这样您就需要手动激活站点上的功能(而不是Visual Studio在调试模式下自动为您执行此操作) - 但调试将完美无缺地工作。

相关问题