5

We want to monitor the "User Profile Changes", so have used "UserProfileChangeCollection". It works perfectly for "UserProfileMultiValueChange" and "UserProfileSingleValueChange" however it is not working if term store is associated with a property.

Could find the below data using Powershell however OM is not recognizing the update information for the term store associated property. Is this expected? or am I missing something here?

ChangedValue : IT
ProfileProperty : Microsoft.Office.Server.UserProfiles.Property
NewValueProfile :
NewValueTerm : Microsoft.SharePoint.Taxonomy.Term
SecondaryValue : IT
ChangedUser : Microsoft.Office.Server.UserProfiles.UserProfile
AccountName : domain\accountName
Id : 12551787
ChangeType : Add
ObjectType : MultiValueProperty
EventTime : 9/29/2011 9:32:51 AM
PrivacyPolicy : Microsoft.Office.Server.UserProfiles.Property
ChangedProfile : Microsoft.Office.Server.UserProfiles.UserProfile


Code used:

private void updateUserProfileChanges(string _mySiteLocal, string _mySiteCentral, string currentUser)
{  
  //string currentUser ="";
  string localLastUpdateDate ="";
  string centalLastUpdateDate ="";
  DateTime localLastUpdateDateTime;
  string msg = "";
  bool isRedirectReq = false;
  SPUserToken userToken;
  //HttpContext currentContext;

  //currentUser = SPContext.Current.Web.CurrentUser.LoginName;
  userToken = SPContext.Current.Web.CurrentUser.UserToken;

  try
  {
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
    using (SPSite localMySite = new SPSite(_mySiteLocal,userToken ))
    {
        SPServiceContext localContext = SPServiceContext.GetContext(localMySite);
        UserProfileManager localProfileManager = new UserProfileManager(localContext); 
            HttpContext currentContext = HttpContext.Current;
        HttpContext.Current = null;


        UserProfile localUserProfile = localProfileManager.GetUserProfile(currentUser); 

        using (SPSite centralMySite = new SPSite(_mySiteCentral,userToken ))
        {
            SPServiceContext centralContext = SPServiceContext.GetContext(centralMySite);
            UserProfileManager centralProfileManager = new UserProfileManager(centralContext);
            UserProfile centralUserProfile = centralProfileManager.GetUserProfile(currentUser); 

            centalLastUpdateDate = centralUserProfile.CurrentChangeToken.ToString();
            //msg = centralUserProfile.GetProfileValueCollection("CellPhone").Count.ToString();
            //msg = centralUserProfile["CellPhone"].ToString();

                if (localUserProfile.GetProfileValueCollection("LocalLastUpdateDate").Count >= 1)
                {
                        localLastUpdateDate = localUserProfile["LocalLastUpdateDate"].ToString();
            }
            else
                {
                localUserProfile["LocalLastUpdateDate"].Value = centalLastUpdateDate ;
                localLastUpdateDate = centalLastUpdateDate ;
                localUserProfile.Commit();
                }

            if(centalLastUpdateDate != localLastUpdateDate )
            {
                msg = "Should be updated..!";
                string[] centralChagneTokens = centalLastUpdateDate.Split(';');
                string[] localChagneTokens = localLastUpdateDate.Split(';');
                string localLastChangeTokenDate = localChagneTokens[2].ToString();

                UserProfileChangeQuery userProfileChangeQuery;
                UserProfileChangeToken userProfileChangeToken;

                //UserProfileChangeDictionary userProfileChangeDictionary;
                UserProfileChangeCollection userProfileChangeCollection ;

                localLastUpdateDateTime = Convert.ToDateTime(localLastChangeTokenDate);
                userProfileChangeQuery = new UserProfileChangeQuery(true, true);
                userProfileChangeToken = new UserProfileChangeToken(localLastUpdateDateTime);

                userProfileChangeQuery.ChangeTokenStart = userProfileChangeToken;
                userProfileChangeQuery.PersonalizationSite = true;
                userProfileChangeQuery.SiteMembership = true;
                userProfileChangeQuery.Anniversary = true;
                userProfileChangeQuery.Colleague = true;
                userProfileChangeQuery.UserProfile = true;

                userProfileChangeQuery.Update = true;
                userProfileChangeQuery.Add = true;
                userProfileChangeQuery.Delete = true;
                userProfileChangeQuery.SingleValueProperty = true;
                userProfileChangeQuery.MultiValueProperty = true;
                userProfileChangeQuery.Custom = true;
                userProfileChangeQuery.UpdateMetadata = true;


                //userProfileChangeCollection = localUserProfile.GetChanges();

                userProfileChangeCollection = centralUserProfile.GetChanges(userProfileChangeQuery);
                //userProfileChangeCollection = centralUserProfile.GetChanges();
                //userProfileChangeDictionary = localUserProfile.UserProfilePropertyValueChange(userProfileChangeQuery);
                //IEnumerator userProfileChangeCollection = userProfileChangeDictionary.GetEnumerator();

                //while (userProfileChangeDictionary.GetEnumerator().MoveNext())
                //{
                //}

                msg  = "Begining loop";
                foreach (UserProfileChange userProfileChange in userProfileChangeCollection )
                {
                    //if (userProfileChange is UserProfileSingleValueChange)
                    //{
                        //UserProfileSingleValueChange propertyChange = (UserProfileSingleValueChange)userProfileChange;                     
                        //localUserProfile[propertyChange.ProfileProperty.Name].Value = propertyChange.NewValue;
                        //msg  = "Inside singlevalue properties";
                    //}
                    //else if (userProfileChange is UserProfileMultiValueChange)
                    //{
                        //UserProfileMultiValueChange propertyChange = (UserProfileMultiValueChange)userProfileChange;
                        //if(propertyChange.ChangeType.ToString() == "Add")
                        //{
                            //localUserProfile[propertyChange.ProfileProperty.Name].Add((Object)"c@d.com");
                        //}
                        //propertyChange will have the new and old value of profile fileds
                        //msg = propertyChange.ChangeType.ToString() + " : " + propertyChange.ProfileProperty.Name;
                        //msg  = "Inside Multivalue properties";
                    //}
                    //msg = userProfileChange.GetType().ToString();
                    msg += userProfileChange.Id.ToString();
                    //msg  = userProfileChange.ProfileProperty.Name;

                    //if (change is UserProfilePropertyValueChange)
                    //{
                        //UserProfilePropertyValueChange propertyChange = (UserProfilePropertyValueChange)change;
                        //msg = propertyChange.ProfileProperty.Name;
                    //}
                }

                // section update term store properities....


                    if (centralUserProfile.GetProfileValueCollection("multiValuePropTest").Count >= 1)
                    {
                            for(int k=0; k< centralUserProfile.GetProfileValueCollection("multiValuePropTest").Count; k++)
                    {
                        //localLastUpdateDate = localUserProfile["LocalLastUpdateDate"].ToString();

                    }
                }               

                //msg = localLastUpdateDateTime.ToString() ;

                //localUserProfile["LocalLastUpdateDate"].Value = centalLastUpdateDate ;
                //localUserProfile.Commit();

                //Page.Response.Redirect(_mySiteLocal);
                //isRedirectReq = true;

                //msg = "Final step";
            }
            else
            {
                //User Profile is upto date and there are no further updates pending...
                //Leaving user control...
                msg = "Upto date..!";
            }
        }

        HttpContext.Current = currentContext;
    }
    });
  }
  catch(Exception ex)
  {
    msg =  ex.Message;  
  }
  finally
  {
    //if (isRedirectReq )
    //  Page.Response.Redirect(_mySiteLocal + "/Person.aspx");
    //HttpContext.Current = currentContext;
    Page.Response.Redirect("http://www.google.com?k=" + msg);
  }
}
2
  • A flag is only seen by a moderator. Try offering a bounty for this question in order to get more visibility to everyone.
    – Kit Menke
    Oct 17, 2011 at 15:44
  • Can you remove the unnecessary or commented code. It will help to focus where the problem is. Also, please mention the output you get with the code above(like you did for powershell) Nov 10, 2011 at 5:44

1 Answer 1

1

Try to use UserProfile.GetChanges(UserProfileChangeToken) instead of UserProfile.GetChanges(ProfileBaseChangeQuery). It will give you broader level of changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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