Step by step SharePoint Development Study Notes Series 10, SharePoint web service Development (II)

Source: Internet
Author: User

 

Summary

 

Next we will introduce Lists. updateListItems update item practices and UserGroup. getUserCollectionFromSite () usage, please first learn Step by Step SharePoint Development Learning notes Series 8, SharePoint web service Development (on), you will be more likely to learn web service Development

 

Lists. UpdateListItems usage

 

Update the xml format of items in a common list

 

View sourceprint? 01 <Batch OnError = "Continue" ListVersion = "1"

 

02 ViewName = "270C0508-A54F-4387-8AD0-49686D685EB2">

 

03 <Method ID = "1" Cmd = "Update">

 

04 <Field Name = "ID"> 4 <Field>

 

05 <Field Name = "Field_Name"> Value </Field>

 

06 </Method>

 

07 <Method ID = "2" Cmd = "Update">

 

08 <Field Name = "ID"> 6 </Field>

 

09 <Field Name = "Field_Name"> Value </Field>

 

10 </Method>

 

11 </Batch>

 

Update the xml format of folder items

 

View sourceprint? 01 <Batch OnError = "Continue" PreCalc = "TRUE"

 

02 ListVersion = "0"

 

03 ViewName = "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">

 

04 <Method ID = "1" Cmd = "Update">

 

05 <Field Name = "ID"> 3 </Field>

 

06 <Field Name = "owshiddenversion"> 1 </Field>

 

07 <Field Name = "FileRef">

 

08 http: // Server/[sites/] [Site/] Shared

 

09 Documents/Folder </Field>

 

10 <Field Name = "FSObjType"> 1 </Field>

 

11 <Field Name = "BaseName"> Name </Field>

 

12 </Method>

 

13 </Batch>

 

Update the item xml format of documents

 

View sourceprint? 01 <Batch OnError = "Continue" PreCalc = "TRUE"

 

02 ListVersion = "0"

 

03 ViewName = "{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">

 

04 <Method ID = "1" Cmd = "Update">

 

05 <Field Name = "ID"> 2 </Field>

 

06 <Field Name = "owshiddenversion"> 1 </Field>

 

07 <Field Name = "FileRef">

 

08 http: // Server/[sites/] [Site/] Shared

 

09 Documents/File </Field>

 

10 <Field Name = "BaseName"> Name </Field>

 

11 </Method>

 

12 </Batch>

 

Processing of xml format when item is updated

 

View sourceprint? 01 /// <summary>

 

02 // Get the approved XML Node that will be used for the insert method of the webservice

 

03 // </summary>

 

04 // <param name = "batch"> </param>

 

05 /// <returns> </returns>

 

06 private XmlNode GetUpdateXmlNode (List <ListItemBE> batch)

 

07 {

 

08 StringBuilder xml = new StringBuilder ();

 

09

 

10 xml. Append (@ "<Batch OnError =" "Continue" "> ");

 

11 foreach (ListItemBE listItem in batch)

 

12 {

 

13 xml. AppendFormat (@ "<Method ID =" "{0}" "Cmd =" "Update"> ", listItem. Id );

 

14 xml. Append (GetFieldInnerXml (listItem ));

 

15 xml. Append ("</Method> ");

 

16}

 

17 xml. Append ("</Batch> ");

 

18

 

19 // Get the Batch node

 

20 XmlDocument doc = new XmlDocument ();

 

21 doc. LoadXml (xml. ToString ());

 

22

 

23 XmlNode batchNode = doc. SelectSingleNode ("// Batch ");

 

24 return batchNode;

 

25}

 

Processing of sharepoint web service called when item is updated

 

View sourceprint? 01 /// <summary>

 

02 // Insert the items

 

03 // </summary>

 

04 // <param name = "batch"> </param>

 

05 /// <returns> </returns>

 

06 private UpdateResultBE UpdateItems (List <ListItemBE> batch)

 

07 {

 

08 // Get the Insert XML Node

 

09 XmlNode batchNode = GetUpdateXmlNode (batch );

 

10 XmlNode result = null;

 

11

 

12 _ logger. Log ("Started batch updating list Items ");

 

13 try

 

14 {

 

15 // Call the webservice

 

16 result = _ ws. UpdateListItems (_ listProperty. ListName, batchNode );

 

17}

 

18 catch (Exception ex)

 

19 {

 

20 _ logger. Log (String. Format ("Error updating Items. Exception: {0}. Stack Trace: {1}", ex. Message, ex. StackTrace ));

 

21}

 

22

 

23 _ logger. Log ("Finished batch updating list items ");

 

24

 

25 // Transform the result into an object

 

26

 

27 UpdateResultBE insertResult = new UpdateResultBE (result, _ listProperty );

 

28 LogInsertResult (insertResult );

 

29

 

30 return insertResult;

 

31}

 

UserGroup. GetUserCollectionFromSite usage

 

First, establish a web service connection. The Code is as follows:

 

View sourceprint? 1 public static SPUserGroupWS. UserGroup GetUserGroupWebService (ListBE listProperty)

 

2 {

 

3 string wsUrl = GetWSUrl (listProperty) + "_ vti_bin/usergroup. asmx ";

 

4 SPUserGroupWS. UserGroup ws = new SPUserGroupWS. UserGroup ();

 

5 ws. Url = wsUrl;

 

6 ws. usedefacrecredentials = true;

 

7 ws. Credentials = WSHelper. GetCredentials (listProperty. Type );

 

8 return ws;

 

9}

 

We only take two simple fields for demonstration.

 

View sourceprint? 1 public class UserInfoBE

 

2 {

 

3 public string ID {get; set ;}

 

4 public string NTLoginName {get; set ;}

 

5}

 

How to obtain the site user list

 

View sourceprint? 01 public List <UserInfoBE> GetUserInfoList ()

 

02 {

 

03 List <UserInfoBE> userInfoList = new List <UserInfoBE> ();

 

04 UserInfoBE listItem;

 

05

 

06 XmlNode nodes = ws. GetUserCollectionFromSite ();

 

07 foreach (XmlNode node in nodes)

 

08 {

 

09 if (node. Name = "Users ")

 

10 {

 

11 for (int I = 0; I <node. ChildNodes. Count; I ++)

 

12 {

 

13 if (node. ChildNodes [I]. Name = "User ")

 

14 {

 

15 listItem = new UserInfoBE ();

 

16 listItem. ID = node. ChildNodes [I]. Attributes ["ID"]. Value;

 

17 listItem. NTLoginName = node. ChildNodes [I]. Attributes ["LoginName"]. Value;

 

18 userInfoList. Add (listItem );

 

19}

 

20}

 

21}

 

22}

 

23 return userInfoList;

 

24}

 

In this way, you can obtain the site user list information through the web service, but note that the web service user name and password must be the administrator of the sharepoint site.

 

Summary

 

Web service users of sharepoint are helpful for data migration of sharepoint sites.

Related Article

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.