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

cho*_*bo2 1 sharepoint sharepoint-2010

我正在关注本教程,我正在尝试在事件接收器中设置代码.

我需要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)
{

}
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用getFullPath但是没有用.我也尝试使用MapPath但我似乎无法访问它.

那我怎么得到XML文件(我认为这就是我需要的)?

Den*_*s G 5

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

不用多说,代码略有不同:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

那么如何将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>
Run Code Online (Sandbox Code Playgroud)

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