Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » SharePoint » Access Sharepoint Portal Server using Web Services in .NET

Access Sharepoint Portal Server using Web Services in .NET

This article discusses how we can leverage Sharepoint Portal Web Services to work with WSS in Microsoft .NET. The sample code used in this article shows how to access Lists Web Service to get the available lists in WSS. The sample code also shows how to add and update list items.

Author Rank :
Page Views : 35653
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction

Sharepoint Portal Server 2003 (WSS) exposes its functionality using a Web Service interface, which has sixteen Web services and each of these services exposes different functionality such as security groups and users, user permissions, documents and imaging, lists, alerts, meetings, sites and site data, versions, views, forms, and web parts. These Web Services are listed in Appendix A in the end of this article.

This article discusses how we can leverage Sharepoint Portal Web Services to work with WSS in Microsoft .NET. The sample code used in this article shows how to access Lists Web Service to get the available lists in WSS. The sample code also shows how to add and update list items.

Adding Web Service Reference

To access WSS Web Services, first thing we need to do is to add reference of the Web Service to the project by right clicking on the project in Solution Explorer and selecting Add Web Reference menu item, which opens Add Web Reference dialog (See Figure 1). In the URL, enter your server URL followed by /_vti_bin/lists.asmx. By default, all WSS Web Services are deployed in _vti_bin folder. As soon you click Go button, you will see all available methods of the Lists Web Service. Now in Web reference name text box type of the name of the reference which you want the reference to be and click on Add Reference button. I call it ListsWS as you can see in Figure 1.

Figure 1.

Now you will see ListsWS under Web References in the Solution Explorer. See Figure 2.

Figure 2.

Once a Web reference is added to your project, you use Lists Web Service as any other Web Service.

The code listed in Listing 1 calls GetListCollection method and adds all available lists to a ListBox control.

Dim listService As ListsWS.Lists = New ListsWS.Lists

listService.Credentials = System.Net.CredentialCache.DefaultCredentials

Dim listNodes As XmlNode = listService.GetListCollection

Dim listName As String

Dim listItemsNode As XmlNode

For Each node As XmlNode In listNodes

     listName = node.Attributes("Title").Value

     ListBox1.Items.Add(listName)

Next

Listing 1.

Now using the same process, you can call other methods to add, update, or delete lists and list items.

Adding Network Credentials

It is possible; your default network credentials may not have access to the Web Service. The following code adds Network Credentials to the Service. You need to replace dummy user name, password, and domain name with your correct user name, password, and domain name.

Dim myCred As New System.Net.NetworkCredential("UserID", "PWD", "Domain")

Dim myCache As New System.Net.CredentialCache

myCache.Add(New Uri("SERVERURI"), "NTLM", myCred)

listService.Credentials = myCache

Listing 2.

Adding and Deleting a List Items

The UpdateListItems method is used to add, update, and delete a list items. In our items list, we have an item called User.

The following code adds a new user to the list.

Dim doc As XmlDocument = New XmlDocument

' Create a Batch element

Dim addUserBatch As XmlElement = doc.CreateElement("Batch")

' Create Batch element XML request with Cmd = New to add a new record

' Other fields are Email, Title, Functional Area, and CT Membership

' Text 'x0020' in the Field is for space

addUserBatch.InnerXml = "<Method ID='1' Cmd='New'>" & _

          "<Field Name='ID'>New</Field>" & _

          "<Field Name='Email'>" + email + "</Field>" & _

          "<Field Name='Title'>" + userName + "</Field>" & _

          "<Field Name='Functional_x0020_Area'>" + fa + "</Field>" & _

          "<Field Name='CT_x0020_Membership'>" + ctm + "</Field>" & _

          "</Method>"

 

' Create an instance of Lists web service

Dim listService As ListsWS.Lists = New ListsWS.Lists

' Call UpdateListItems method to add new list item

Dim returnNode As XmlNode = listService.UpdateListItems(listName, addUserBatch)

Dim resultNode As XmlNode = returnNode.FirstChild

For Each childNode As XmlNode In resultNode.ChildNodes

For Each attrib As XmlAttribute In childNode.Attributes

            If (attrib.Name = "ows_SelectTitle") Then

                   newCreatedUserId = attrib.Value

                  Exit For

                 End If

     Next

Next

Listing 3.

The following code deletes an existing user from the list items.

' Create a Batch element

Dim addUserBatch As XmlElement = doc.CreateElement("Batch")

' Create Batch element XML request with Cmd = Delete and pass UserID

addUserBatch.InnerXml = "<Method ID='1' Cmd='Delete'>" & _

      "<Field Name='ID'>" + userId + "</Field>" & _

       "</Method>"

 

' Create an instance of Lists web service

Dim listService As ListsWS.Lists = New ListsWS.Lists

' Call UpdateListItems method to add new list item

Dim returnNode As XmlNode = listService.UpdateListItems(listName, addUserBatch)

Listing 4.

Summary

 

The Web Services interface of Windows Sharepoint Portal Server 2003 is one way to access and manipulate Sharepoint database. In this article, I discussed how we can leverage Lists Web Service to retrieve, add, and update Lists and List items using Microsoft .NET.

 

Appendix A: Windows Sharepoint Portal 2003 Web Services

 

The following summarizes these Web services.

 

  1. Administration Service - This service provides functionality for managing Sharepoint Services such as creating and deleting site collections.
  2. Alerts Web Service - This service provides functionality to work with alerts in a Sharepoint site.
  3. Document Workspace Web Service - This service provides functionality for managing document workspace sites and the data they contain.
  4. Forms Web Service - This service provides functionality to return forms that are used in the user interfaces when working with the contents of a list.
  5. Imaging Web Service - This service provides functionality to create and manage picture libraries.
  6. List Data Retrieval Service - This service provides functionality to perform queries against lists in the Sharepoint Services.
  7. Lists Web Service - This service provides functionality to work with lists and list data Sharepoint Services.
  8. Meeting Web Service - This service provides functionality to create and manage Meeting Workspace sites.
  9. Permissions Web Service - This service provides functionality to create and manage Meeting Workspace sites.
  10. Site Data Web Service - This service provides functionality to return metadata or list data from sites.
  11. Sites Web Service - This service provides functionality to return information about the collection of site templates on the virtual server.
  12. Users and Groups Web Service - This service provides functionality for working with users, site groups, and cross-site groups.
  13. Versions Web Service - This service provides functionality to work with file versions.
  14. Views Web Service - This service provides functionality to work with views of lists.
  15. Web Part Pages Web Service - This service provides functionality to send information to and retrieve information from XML Web services.
  16. Webs Web Service - This service provides functionality to work with sites and subsites.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Store data in to list through webpart by aarthi On July 24, 2009


Hi Friends

I am new to sharepoint and please help me. I have developed a web part through web user control and deployed the webpart in to sharepoint. My webpart contains few textboxes, dropdown, listbox and a submit button. When i click the button, details from the controls(textbox,dropdown,list) should be taken and stored in the sharepoint list. Is it Possible ? Could please help me by giving sample codes. Thanks in advance!

Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.