Sharepoint client development: adds user information to the user information list

Source: Internet
Author: User

User Information List, a hidden Sharepoint list, is used to store basic user profile data. This list cannot be added or deleted in normal mode.

First, why add data to this list?

This list is managed by Sharepoint. When the zookeeper icker control is used, users are automatically added to the user information list. For the user type field in the list, this will be associated with the user information list using ID.

I have met two typical scenarios:

    1. In actual user operations, Datasheet view is generally used for batch data import and is imported through Excel. If Michael has never visited the website, and Michael has been imported, The Datasheet view reports an error. Unable to save.
    2. Develop cross-site data migration and synchronization. Michael is a user of website a, but not website B. She needs to migrate data related to Michael to website B.

In the first scenario, users can randomly find a javasepicker control, enter the missing person information, and click it to add it back.

 

For developers, you can use spweb. ensureuser to add users to this list.

Corresponding client development:

    • For Versions later than SharePoint 2010, client om also has the corresponding web. ensureuser method.
    • The people. resolveprincipals method can be used for traditional Web Services.

In most cases, the previous client om can meet your needs.

 

However, the problem with Web. ensureuser is that only one user can be added at a time, and this is a complete service request, which is very efficient when there is a large amount of data.

To solve this problem, multithreading is required.

Here I have a piece of actually usedCodeFor reference:

I encountered a problem during the actual use of multithreading. Refer to the methods described in this discussion:Workaround for the waithandle. waitall 64 handle limit?

UserinfolistTargetlist =New Userinfolist(Parameters. TargetUrl,Parameters.Listnames. Userinformation );

Ilist<User> Resolvedusers =New List<User> ();

Logger. Info ("Sync user information from {0} to {1} started ...",Parameters. Sourceurl,Parameters. TargetUrl );

IntThreadcount = 0;

ManualreseteventFinished =New Manualresetevent(False);

Foreach(VaRUInUsers)

{

Interlocked. Increment (RefThreadcount );

Threadpool. Queueuserworkitem (Delegate

{

Try

{

Logger. Info ("Resolving user in target web site :", U. sipaddress, U. Account );

If(!String. Isnullorempty (U. Account) & targetlist. getuserinfobyaccount (U. Account) =Null)

{

UserTargetuser = targetlist. Web. ensureuser (U. Account );

Targetlist. Context. Load (targetuser );

Targetlist. Context. executequery ();

Resolvedusers. Add (targetuser );

Logger. Info ("User resolved :", U. sipaddress, U. Account );

}

Else

{

Logger. Info ("User exists or user is not valid :", U. sipaddress, U. Account );

}

}

Catch(Exception)

{

Logger. Info ("Cannot update user information for {0} ({1 })", U. sipaddress, U. Account );

}

Finally

{

If(Interlocked. Decrement (RefThreadcount) = 0)

{

Finished. Set ();

}

}

});

}

Finished. waitone ();

Logger. Info ("Sync user information from {0} to {1} completed ...",Parameters. Sourceurl,Parameters. TargetUrl );

 

 

If you are not familiar with multithreading, you can only use the traditional Web Service to improve performance. Use the people. resolveprincipals method because the parameters of this method allow arrays. Multiple users can be transferred in an HTTP request, greatly saving the HTTP round-trip time.

Similarly, I have a piece of code that I actually use as a reference:

When using this service, you cannot add too many users at a time. This will cause the Web service to time out and fail.

Public Userinfo[] Ensureusers (String[] Accounts,IntBatch = 200,StringUrl =Default(String))

{

If(Url =Default(String) {Url = weburl ;}

Logger. Info ("Ensuring user information in {0} web site", URL );

List principalinfo Pi = New List principalinfo >> ();

For(IntI = 0; I <Math. Ceiling ((Double) Accounts. Length/batch); I ++)

{

String[] Batchaccounts = accounts. Skip (I * batch). Take (batch). toarray ();

PeoplePeopleservice =New People();

Peopleservice. url = URL +"/_ Vti_bin/people. asmx";

Leleservice. Credentials = system. net.Credentialcache. Defaultcredentials;

Pi. addrange (peopleservice. resolveprincipals (batchaccounts,Spprincipaltype. User,True));

}

List<Userinfo> Users = convert (PI );

Logger. Info ("Found {0} user information items and updated to user information store", Users. Count );

ReturnUsers. toarray ();

 

}

 

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.