Friday, February 19, 2010

SharePoint Web Service(3)--Add a New List Item with Hyperlink and

In previous posts, I only give some simple examples about how to use the web serive to add a new list item only with text type column. Now I want to show you how to add a new list item with Hyperlink and Person  type columns.
First, I want to talk something about the Hyperlink type column. If you have already used the Object Model to read a hyperlink type field before, you would found that if you use the toString() function, you would get a string whose format is "url,description". I need use it later.
string strBatch = @"<Method ID='1' Cmd='New'>
                                <Field Name='Title'>Sample Title</Field>
                                <Field Name='Url'>http://www.microsoft.com, Microsoft Site</Field>

                                </Method>";
If you don't know how to use this code snippet, please see my previous post about the SharePoint web service.
Adding a new item with Person type column would be a little complicated compared to hyperlink. You also can use the toString() to see what's the format it is. Then you would find that the format is"ID;#domain\\account". When you use the web service, you can simply use the format "ID;domain\\account".So you need know the ID first, you can use another service Usergroup.asmx.

UserGroupWebService.UserGroup ugService=new UserGroupWebService.UserGroup();
 
XmlNode ugNode
=ugService.GetUserInfo("Server\\LoginName");
            XmlDocument ugdoc 
= new XmlDocument();
            ugdoc.LoadXml(ugNode.OuterXml);
            XmlNodeList ugList 
= ugdoc.GetElementsByTagName("User");
            string id = ugList[0].Attributes["ID"].Value;
Then you can use the ID to add a new item with a person type field like below:
<Field Name='User'>ID;#domian\\account</Field>

No comments:

Post a Comment