WebService读取 SharePoint列表

来源:互联网 发布:局域网网络监视软件 编辑:程序博客网 时间:2024/05/22 02:39

提示虚拟一个管理员。

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

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

clip_image002

2、 添加引用

添加Microsoft.SharePoint.dll文件的引用,因为我们需要使用SharePoint的对象模型来读取列表信息,Dll文件的位置是C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll

clip_image003

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, string ListName, 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后测试

clip_image004

5、 使用两个函数

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

clip_image005

clip_image006

6、 调用结果

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

clip_image008

clip_image010



纯手写:群 212099235


原创粉丝点击