Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created an event receiver that does two things:
1) Pulls information from a selected user's profile and populates a column with that information (BC Champion ID). <-- This part works
2) Extracts the first Cost Center from a list of Cost Centers (Column is named 'Cost Center(s)') and populates a column, (CC) <-- This part does not work.

Stepping through the code, I can see that properties.ListItem["CC"] is populated like I would expect before ListItem.Update() but afterward, the column is slightly changed, such that preceeding 0s are truncated and ;# is added to the end of the string. Although properties.ListItem["CC"] seems to be populated via the event receiver, it is indeed blank when I check the list item. Any ideas?

public override void ItemUpdated(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);

       EventFiringEnabled = false;
       string userID = properties.ListItem["BC Champion"] as string;
       //string userID = properties.AfterProperties["BC_x0020_Champion"] as string;
       SPFieldUserValue userName = new SPFieldUserValue(properties.Web, userID);
       string selectedUser;
       if (userName.User != null)
       {
           selectedUser = userName.User.LoginName;
       }
       else
       {
           selectedUser = userName.LookupValue;
       }

       var user = GetUserInfo(selectedUser);
       properties.ListItem["BC Champion ID"] = user["corpID"].Value;
       //properties.AfterProperties["LookupBU"] = getLookUpValue(user["BU"].Value.ToString(), properties.List, "LookupBU");

       string CCs = properties.ListItem["Cost Center(s)"].ToString();
       string[] parts = CCs.Split(new string[] { ";#" }, StringSplitOptions.None);
       string primaryCC = parts[1];
       properties.ListItem["CC"] = primaryCC as string;
       properties.ListItem.Update();
       EventFiringEnabled = true;
   }

   private UserProfile GetUserInfo(string accountName)
   {
       using (SPSite site = new SPSite("http://sp2010/sites/MyBCP2"))
       {
           SPServiceContext context = SPServiceContext.GetContext(site);
           UserProfileManager profileManager = new UserProfileManager(context);

           UserProfile testUser = profileManager.GetUserProfile(accountName);
           return testUser;
       }
   }



Watch before ListItem.Update()

CCs                       "1304;#02163;#107;#01006"
primaryCC                 "02163"
properties.ListItem["CC"] "02163"

Watch after ListItem.Update()

CCs                       "1304;#02163;#107;#01006"
primaryCC                 "02163"
properties.ListItem["CC"] "2163;#"
share|improve this question
    
not sure why you are first calling base.ItemAdding(prop) in your ItemUpdated event handler or would it matter? –  Supriyo SB Chatterjee Jun 5 '12 at 13:47
    
Thanks! I took it out but I see the same behavior... –  James Grizzle Jun 5 '12 at 14:02
add comment

2 Answers

My guess would be that the Column "CC" is defined ad a lookup column and when you give that just a string with a number instead of a SPFieldLookupValue rightly constructed it assumes that the number is just the ListId of the row in the lookup table.

The right value to assing "CC" is probably "1304;#02163"

But you should populate it using code like this:

var CCs = new SPFieldLookupValueCollection((string)properties.ListItem["Cost Center(s)"]);
properties.ListItem["CC"] = CCs[0];
share|improve this answer
    
Good Guess but CC is just a single line of text column. The 'Cost Center(s)' column is a lookup column though. –  James Grizzle Jun 5 '12 at 13:52
    
If it really is a Single line of text then you should report the bug to MS, but I don't believe you –  Per Jakobsen Jun 5 '12 at 15:11
    
I checked again. I promise it is a single line of text field... =( –  James Grizzle Jun 5 '12 at 17:35
add comment
up vote 0 down vote accepted

I ended up just deleting and recreating the column with another name and changing my code to reference the new column. It works fine. I never found out what the issue was with the original column.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.