c# - Add a new list item with a Person or Group field -


i have datatable, , want add new items sharepoint list using data in table's datarows. i'm having trouble adding data field type "person or group", because field seems require id, not "lastname, firstname". made class build caml call updatelistitems(), works fine adding other type of data list:

public class camlbuilder {     list<string> namefields;      public camlbuilder()     {         namefields = new list<string>();         namefields.add("requestor");     }      public xmlelement batchaddelement(datatable addtable)     {         xmldocument doc = new xmldocument();         xmlelement element = doc.createelement("batch");         element.setattribute("onerror", "continue");         element.setattribute("listversion", "1");         element.setattribute("viewname", "");         int methodctr = 1;         string batchstr = "";         foreach (datarow dr in addtable.rows)         {             batchstr += "<method id='" + methodctr + "' cmd='new'>";             foreach (datacolumn dc in addtable.columns)             {                 batchstr+="<field name='"+dc.columnname+"'>" + convertname(dc.columnname,convert.tostring(dr[dc])) + "</field>";             }             batchstr+="</method>";             methodctr++;         }         element.innerxml = batchstr;         return element;     }      private string convertname(string columnname,string name)     {         if (namefields.contains(columnname))         {             name = "-1;#" + name;         }         return name;      }  } 

the "-1;#" in convertname() saw in this discussion workaround lets avoid looking person's id. doesn't work; error message in returned xml:

0x81020054the user not exist or not unique. 

is there way build caml don't need id , can use name?

my solution far use people.asmx web service. following code assumes have created reference web service called peopleservice, , instance of service called ps:

public string findpersonid(string searchtext)         {             peopleservice.principalinfo[] arrayprincipalinfo = ps.searchprincipals(searchtext, 10, peopleservice.spprincipaltype.all);             if (arrayprincipalinfo.count() > 0)             {                 return convert.tostring(arrayprincipalinfo[0].userinfoid);             }             else             {                 return string.empty;             }         } 

this not ideal, because requires every row in datatable must call web service separately (maybe optimized grouping identical names , calling function once each unique name), lot of web traffic if there lot of names. else has better solution.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -