Friday, 11 May 2012

Copying Multiple User Field from one SharePoint List to another

So once again I landed up in trouble because of the Person or Group field (Multiple Selection). Refer earlier post.
Now, this time what I needed to do is I simply needed to copy a Person or Group field with multiple selection enabled to another such column in another list, through an Event Receiver.
You might think - "Why is he writing an event receiver?? Why can't he just use a simple SPD Workflow??"
But as usual.......... there is a catch!!
I have created a SharePoint site with Forms Based Authentication (FBA). My users no longer come from the Active Directory, they come from an SQL Membership API database.
And.... SPD Workflows run under the OWSTIMER.EXE service not the w3wp.exe service.
(For general information the w3wp service is created by the IIS and is used to handle web requests coming to a web server. The OWSTIMER service is created by the Windows SharePoint Timer Service and is used to execute scheduled jobs of SharePoint.)
So, since the SPD Workflows run under a different service, they cannot read the web.config placed under the IIS Service, therefore, they cannot read the Membership API database settings, therefore, SPD Workflows cannot identify FBA users.
So, I write a little bit of code to perform the task at hand i.e. copying multiple users from one column to another (using event receiver).
But again..... things are not as simple as you think. I write this line of code:
oDestinationList["MultiUser"] = oSourceList["MultiUser"];
  • DestinationList is Destination List object
  • SourceList is Source List object, and
  • MultiUser is the name of the field
but this simple line of code doesnt work. It states that the source and destination objects are not of the same type and cannot copy code!!!! Why????? I have no clue!
On googling I found a long procedure for copying multiple user fields: Create a SPFieldUserValueCollection object and append user names in this object etc.etc..... thats too much work...
So, on doing a little bit of research I found that there IS a simpler way of achieving this. Here's the code:
oDestinationList["MultiUser"] = Convert.ToString(oSourceList["MultiUser"]);
and it worked!!!! Hurray!!!!
Really SharePoint is funny!! :)

No comments:

Post a Comment