加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService读取 SharePoint列表

发布时间:2020-12-17 00:25:47 所属栏目:安全 来源:网络整理
导读:提示虚拟一个管理员。 ? ? SPSecurity.RunWithElevatedPrivileges(delegate() ? ? ? ?//虚拟管理员,否则其他用户访问没有权限 1、 新建一个WebService的项目,选择Asp.net Web服务应用程序,然后输入项目名称 2、 添加引用 添加Microsoft.SharePoint.dll文

提示虚拟一个管理员。

? ? SPSecurity.RunWithElevatedPrivileges(delegate() ? ? ? ?//虚拟管理员,否则其他用户访问没有权限

1、 新建一个WebService的项目,选择Asp.net Web服务应用程序,然后输入项目名称

2、 添加引用

添加Microsoft.SharePoint.dll文件的引用,因为我们需要使用SharePoint的对象模型来读取列表信息,Dll文件的位置是C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPIMicrosoft.SharePoint.dll

3、 添加函数

函数前面写上[WebMethod]标记,然后定义我们自己需要的方法,方法的参数和返回值,在方法里面,写我们的调用,我的代码就是个简单的例子,返回的也都是String类型,如果操作失败,则返回为空。

GetListItem(string WebUrl,string ListName,int ID)//获取列表项,传参分别为网站地址、列表名、ID

public string GetWebID(string WebUrl)//获取网站的WebID属性

[WebMethod]
public string GetWebID(string WebUrl)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate() ? ? ? ?//虚拟管理员,否则其他用户访问没有权限
? ? ? ? ? ? ? ? {
SPSite site = new SPSite(WebUrl);
SPWeb web = site.OpenWeb();
return web.ID.ToString();
}
catch (Exception ex)
{
return "";
}
}
}
[WebMethod]
public string GetListItem(string WebUrl,int ID)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate() ? ? ? ?//虚拟管理员,否则其他用户访问没有权限
? ? ? ? ? ? ? ? {
SPSite site = new SPSite(WebUrl);
SPWeb web = site.OpenWeb();
SPListItem item = web.Lists[ListName].Items[ID];
String rt = "标题:" + item["Title"].ToString() + "内容:" + item["内容"].ToString();
return rt;
}
catch (Exception ex)
{
return "";
}
}
}


发布到IIS以后,打开我们的WebService,可以看到我们的自定义的两个方法,都在WebService中了。4、 发布WebService后测试

5、 使用两个函数

我们可以分别测试下我们的两个函数,点击我们的函数,可以进入下面的界面,输入我们的参数,调用即可。

6、 调用结果

如果,可以看出我们调用的结果,下面第一张图是GetListItem(string WebUrl,int ID)函数的,第二张图是GetWebID(string WebUrl)的结果。



纯手写:群?212099235

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读