To read a SharePoint list item by customizing WebService

Source: Internet
Author: User
Tags object model reference tostring

To provide integration to other systems, found that the webservice of SharePoint with a variety of different, simply write their own point, but also as a record of their own study. Of course, the content is relatively simple, I hope heroes do not mind, do not scold me ah. All right, let's get to the point.

1. First, use VS2008 to create a new WebService project, select the ASP.net Web Service application, and then enter the project name

2. Add Reference

Add a reference to the Microsoft.SharePoint.dll file because we need to use the SharePoint object model to read the list information, where the DLL file is C:\Program files\common Files\Microsoft Shared\Web Server Extensions\12\isapi\microsoft.sharepoint.dll

3. Add function

The function is preceded by a [WebMethod] tag, and then defines the method we need, the parameter and the return value of the method, the method in which to write our call, my code is a simple example, and the return is a string type, and if the operation fails, the return is null.

Getlistitem (string weburl,string listname,int ID)//Get list items, including Web address, list name, ID

public string Getwebid (string weburl)//Get WebID property of Web site

[WebMethod]
public string GetWebID(string WebUrl)
{
try
{
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
{
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 "";
}
}

4, Post WebService test

After you publish to IIS, open our webservice and see our custom two methods, all in WebService.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.