Geeks With Blogs

News

Matthew Cassell a windows developer writing asp .net web apps

I've been developing a web application recently which needed to access a calendar in a sharepoint installation to retrieve events and display them. Under the covers in sharepoint, a calendar with events is more or less just a list. So we access the list. Here's how I did it:

Step 1

Add a reference to the lists sharepoint web service in Visual Studio:

Step 2

Next we want to initialize a new instance of the Lists class in the referenced web service, and set the credentials to access the sharepoint site:

sharepoint.lists.Lists l = new sharepoint.lists.Lists();

//Supply the credentials to access sharepoint

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("your username here", "your password here");

l.Credentials = cred;

Step 3

In order to retrieve the calendar events, we must get a list of items that belong to the calendar's list. We do so using this piece of code:

XmlNode n = l.GetListItems("your calendar name here", null, null, null, null, null, null);

GetListItems takes a number of parameters, but the only needed one is the name of the calendar, the rest can be null unless you need them. This method returns an XML node with a collection of XML Child Nodes each one representing a list item, and in this case calendar items. An example of the XML it returns through the InnerXml property is this:

<rs:data ItemCount="2" xmlns:rs="urn:schemas-microsoft-com:rowset">

  <z:row ows_EventDate="2007-08-02 20:30:00" ows_EndDate="2007-08-03 00:30:00" ows_fRecurrence="0" ows_EventType="0" ows_Attachments="0" ows_WorkspaceLink="0" ows_Title="Consultants Night" ows_Location="ObjectSharp Training Room, 1 Yonge St., 19th Floor" ows_Description="<div></div>" ows_fAllDayEvent="0" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_owshiddenversion="1" ows_UniqueId="1;#{3912FED0-05B0-4B80-B452-2CF04C148FEC}" ows_FSObjType="1;#0" ows_Created="2007-07-29 21:24:47" ows_FileRef="1;#Lists/Calendar/1_.000" ows_MetaInfo="1;#" xmlns:z="#RowsetSchema" />

  <z:row ows_EventDate="2007-08-22 17:00:00" ows_EndDate="2007-08-22 17:00:00" ows_fRecurrence="0" ows_EventType="0" ows_Attachments="0" ows_WorkspaceLink="0" ows_Title="Test" ows_Description="<div></div>" ows_fAllDayEvent="0" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_owshiddenversion="1" ows_UniqueId="2;#{282B92B3-4B9B-4F78-B3CB-E8B9633793A1}" ows_FSObjType="2;#0" ows_Created="2007-08-22 16:29:59" ows_FileRef="2;#Lists/Calendar/2_.000" ows_MetaInfo="2;#" xmlns:z="#RowsetSchema" />

  </rs:data>

 

Step 4

We can extract the data of each individual event through iterating through each Xml child node, and outputting the requested values. An example of doing this is this:

for(int i = 0; i < n.ChildNodes[1].ChildNodes.Count;i++)

{

if(n.ChildNodes[1].ChildNodes[i].Attributes!=null)

Console.WriteLine("Event " + n.ChildNodes[1].ChildNodes[i].Attributes["ows_Title"].InnerText + " begins on " + n.ChildNodes[1].ChildNodes[i].Attributes["ows_EventDate"].InnerText);

}

 

This will produce the following output in the console window:

Event Consultants Night begins on 2007-08-02 20:30:00

Event Test begins on 2007-08-22 17:00:00

 

Thus we are done!

Posted on Wednesday, August 22, 2007 4:43 PM | Back to top


Comments on this post: Accessing Sharepoint Data through Web Services

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Good stuff and you save my day or two
Left by Narasimha Rao on Nov 12, 2008 3:32 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
hello narasimha
my aim is to fetch contact list data using asp.net application.for that i have done this.

the steps i followed are
1.created a new lists called contacts in sharepoint server 2007 portal.
2.add some data to it.
3.creata a asp.net website.
4.add web referance to http://server_name/_vti_bin/Lists.asmx
5.add u'r code in the page load event.
but i got a exception like this
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

what should i do?can you help me to achieve this objective.i'm not a expert in this field.so will u b kind enf to give me detailed step
by step instruction.
THANKS IN ADVANCE :)
Left by Harikrishnan on Nov 20, 2008 12:57 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
mr Cassell
can you please tell me how can i bring the contact list data to a list box in asp.net.
Left by harikrishnan on Nov 20, 2008 1:03 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
I am also facing same problem
like
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown
Left by Mohit Jethva on Jun 11, 2009 7:48 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Mr. harikrishnan,

I am stuck just like u r situation....How did u solve this for asp.net web application..let me know step by step details...thanks....
You can mail me at fasttoshiba@gmail.com
Left by fast on Jul 29, 2009 5:46 PM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Is there a way to copy an event from one calendar to another?
Left by Pierre on Aug 28, 2009 9:19 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
I always get this error . I tried to change settings like <impersonate////> in web.config and all, but nothing works
The request failed with HTTP status 401: Unauthorized.
Left by Joshi on Sep 08, 2009 6:51 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Not sure how you are calling the Web Services, but need to setup the code as follows:

Step 0: Within Visual Studio, setup the web reference, in this example ListWebReference. Setting upa Web Reference is left

Step 1: Setup the appropriate 'using' statements within your code

Step 2: Instrument code to talk with SharePoint Web Services. (See example: http://moss/sample)

_ListService = new ListWebReference.Lists();
_ListService.Credentials = System.Net.CredentialCache.DefaultCredentials;
_ListService.Url = @"http://moss/sites/sample/_vti_bin/Lists.asmx";

Step 3: Get Contacts

XmlNode items = null;
items = _ListService.GetListItems("Contacts", string.Empty, null, null, string.Empty, null, string.Empty);
Left by David Blaszyk on Dec 07, 2009 6:06 PM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
The request failed with HTTP status 401: Unauthorized.

This is common error if userid and password are not matching. you can use
step 1)System.Net.NetworkCredential oNetworkCredential = new System.Net.NetworkCredential(UserID, Password,"Domain");
oList.Credentials = oNetworkCredential;

If this dosen't work try using step 2) (i am sure this wil work)
Step 2)oList.Credentials = System.Net.CredentialCache.DefaultCredentials;

Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown
This error comes when parameter name is incorrect. do one thing copy and paste the list or view name. it will work for you
Left by Chirantan on Feb 25, 2010 6:19 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Hi,

I created a web application using asp.net which collect data and send to share point list. The share point site is inside firewall. If I put the web application inside firewall, it works fine. But if I put it outside of firewall I will get message: Could not establish trust relationship for the SSL/TLS secure channel. I don't quite understand of firewall or certificate. But I think that maybe the problems.
Would you please tell me what I should do in detail?

Thanks,

Meg

The codes as following

ListService.Url = "https://.../_vti_bin/lists.asmx"
ListService.Credentials = New System.Net.NetworkCredential("user name", "password", "domain")

Dim strBatch As String = String.Empty
strBatch += String.Format("<Method ID='{0}' Cmd='New'>", 1)

.......

strBatch += "</Method>"

Dim xmlDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
Dim elBatch As System.Xml.XmlElement = xmlDoc.CreateElement("Batch")


elBatch.InnerXml = strBatch
Dim ResultOfService As String = _
ListService.UpdateListItems("list name)", elBatch).InnerText

Left by Meg on Jun 03, 2010 3:30 PM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
@David Blaszyk

Your solution, explicitly setting the web service URL, solves my 'SoapServerException' problem. But I'd like to know why.

When I added my web reference, I specified the URL. Why do I need to explicitly state it again?
Left by Alex on Jul 30, 2010 11:07 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
@ Alex.

Visual Studio seems to ignore what you add in to your service reference and always puts in the top level site. Change the config file and put it http://www.mysite.com/teamsite. Then when you do the query you shouldn't have to explicitly add your siteaddress.
Left by Andy on Aug 11, 2010 5:00 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
Hi,
While adding my sharepoint data service url as application service reference i am getting the following error shown in below image.

http://cid-71a91043ae5689e9.office.live.com/self.aspx/.Documents/error.png

The problem is with _HistoryItem in this site. But i dont know how this list created in my site and dont know how to delete those list and list items. I can see this list once in given and validated the site Url in the Add service reference dialog. Please guide me on this issue.

Thanks,
Dhileep
Left by dhileep on Dec 19, 2010 12:08 PM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
I am trying to create a new custom list using SP web services with below code and am not able to create it.

Lists ws = new Lists();


ws.PreAuthenticate = true;

ws.Url = "http://kor300949v19:1000/_vti_bin/Lists.asmx" + " /_vti_bin/lists.asmx";
ws.Credentials = CredentialCache.DefaultCredentials;

XmlNode ndList = ws.AddList("MyList", "List created", 100);

Error : "Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'. The request failed with the error message: An unexpected error has occurred.
Could you please anyone check and give feedback ?
Left by Saran on Dec 28, 2010 4:00 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
I am unable to add the web service in my Solution, Its asking me credentials again n again.
help...
Left by SharePoint Developer on Feb 28, 2011 6:15 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
how to get vale from "ows_Description" with out div or any html tag.
Left by Anuj Kumar on Jul 20, 2011 9:03 AM

# re: Accessing Sharepoint Data through Web Services
Requesting Gravatar...
i am using following code ;

Sharepointlist.Lists listService = new Sharepointlist.Lists();

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("123", "",""); //UserId, pws

//listService.Credentials = cred; //System.Net.CredentialCache.DefaultNetworkCredentials;

listService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

listService.Url = "http://----/_vti_bin/lists.asmx";


XmlNode n = listService.GetListItems("ListName", null, null, null, null, null);

int i1 = n.ChildNodes[1].ChildNodes.Count;

for (int i = 0; i < n.ChildNodes[1].ChildNodes.Count; i++)
{

if (n.ChildNodes[1].ChildNodes[i].Attributes != null)

Console.WriteLine("Event " + n.ChildNodes[1].ChildNodes[i].Attributes["Title"].InnerText);

}

but getting error on the below line:

Console.WriteLine("Event " + n.ChildNodes[1].ChildNodes[i].Attributes["Title"].InnerText);
Error : "Object reference not set to an instance of an object."

have you any idea?
Left by Rohit on Sep 27, 2012 3:33 AM

Your comment:
 (will show your gravatar)
 


Copyright © Matthew Cassell | Powered by: GeeksWithBlogs.net | Join free