Wednesday, September 2, 2009

Getting Announcement from SharePoint 3 using web service

I am writing because I had to digg, may be other won't have to spent more time like me.

My need:
I needed a page separate from sharepoint to list announcement.

My problem:
I am new to sharepoint and never did program with sharepoint.

What I did:
Of course, I googled! and here are the references first:
1. Get list items from sharepoint
2. Display xml data
3. Get attachment url

I first added a web reference:
1. Right click on Project name is 'Solution Explorer'
2. Choose "Add Web Reference...", 'Add Web Reference' window will open.
3. In 'URL:' box type the sharepoint address similar to http://testBox/dev/_vti_bin/Lists.asmx
4. It will give you error on right panel under 'Web services found at this URL:' saying "The document at the url http://testBox/dev/_vti_bin/Lists.asmx was not recognized as a known document type. ..." and 'Add Reference' button disabled.
5. Add ?WSDL or click on "Service Description" in the left panel to avoid error described in 4.
6. After no. 5 you will be able to see web service(s) in the given url and add the web reference.

Now move on to the second part. Once the web reference is added, there will be 'Web References' in solution explorer.
- Now follow the steps in reference 1, it will pull all the information in the list; I used "Announcements" for my purpose.



protected void Page_Load(object sender, EventArgs e)
{
string webPart = "http://testBox/dev";
XmlNode xmlData = GetItems(webPart);
- To display what you got, follow codes in reference 2. I first printed on scree with Response.Write. At this point you can see xml data returned by web service


protected void Page_Load(object sender, EventArgs e)
{
string webPart = "http://testBox/dev";
XmlNode xmlData = GetItems(webPart);

ChildDisplay(xmlData, 0);
}

- In ChildDisplay function you can filter your required data or do whatever your need be with the obtained data.
- As for the reference 3, I took

listQueryOptions.InnerXml = "<IncludeAttachment>TRUE</IncludeAttachment>";
to get url for the attachment.
- I will need to download and store the attachment in to a folder.


No comments: