繁体   English   中英

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

[英]How to retrieve data from an XML file in a Sharepoint 2010 Feature Event Receiver?

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

我需要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. 您需要处理SPSite / SPWeb对象,这通常是在using子句中完成的。
  2. 您无需在功能接收器中使用绝对路径(硬代码),因为该功能已在网站/网站范围内
  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);
    }
}

那么如何将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文件添加到该模块中(上下文菜单>添加现有项),此功能才能起作用。
还要注意,您可以轻松调试功能接收器,只需确保将部署配置设置为“不激活”(“项目属性”>“共享点”>“活动部署配置”)-这样,您将需要在站点上手动激活功能(代替的Visual Studio在调试模式下自动为您完成此操作)-但调试将完美无缺。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM