ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Showing posts with label Services development. Show all posts
Showing posts with label Services development. Show all posts

Wednesday, June 4, 2014

Sharepoint 2010 Synchronize user profiles value to user information lists

Hello EveryBody,

I found the solution to update data in "User Information List"
Go to Site Action >> Site Settings >> Lists Settings >> create column MicrosoftTechnology_MyFavoriteDocuments type is Multiple lines of text (Plain text )

At Central Admin >> link to servername:port/_admin/ServiceApplications.aspx >> click to User Profile Service Application >> Manage User Properties >> New Properties >> name is MicrosoftTechnology_MyFavoriteDocuments.

Edit any user's information

Here is Application Page (not webpart)

Code Inline

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SyncFavorFromCAToList.aspx.cs"
    Inherits="MicrosoftTechnology.Intranet.Layouts.FVH.SyncFavorFromCAToList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Sync value From CA To User Information List</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Literal ID="litResult" runat="server"></asp:Literal>
    <asp:Button ID="btnSync" runat="server" Text="Sync Favorite properties value From user profile To User Information List" OnClick="btnSync_Click"/>
    </form>
</body>
</html>

Code Behide

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace MicrosoftTechnology.Intranet.Layouts.FVH
{
    public partial class SyncFavorFromCAToList : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }
        protected void btnSync_Click(Object sender, EventArgs e)
        {
            SyncMyFavoriteInUserInformationList();
        }
        public void SyncMyFavoriteInUserInformationList()
        {
            litResult.Text = "";
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb spWeb = site.OpenWeb())
                {
                    try
                    {
                        ServerContext context = ServerContext.GetContext(site);
                        UserProfileManager profileManager = new UserProfileManager(context);
                        foreach (UserProfile userProfile in profileManager)
                        {
                            bool existingMyFavoriteDocuments = false;
                            string myFavoriteValue = string.Empty;                                                      
                            UserProfileValueCollection valueCollection = userProfile["MicrosoftTechnology_MyFavoriteDocuments "];
                            foreach (object value in valueCollection)
                            {
                                myFavoriteValue += value.ToString() + ";";
                                existingMyFavoriteDocuments = true;
                            }
                            if (existingMyFavoriteDocuments==true)
                            {
                                UpdateMyFavoriteInUserInformationList(spWeb, Convert.ToString(userProfile["AccountName"]), myFavoriteValue);
                                litResult.Text += "<br />Added Successful<br />";
                            }  
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }
            }
        }

        private void UpdateMyFavoriteInUserInformationList(SPWeb spWeb, string accountName, string myFavoriteDocuments)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate(){
                SPList spList = spWeb.SiteUserInfoList;
                SPQuery spQuery = new SPQuery();
                spQuery.Query = string.Format(@"<Where>
                                                      <Eq>
                                                         <FieldRef Name='Name' />
                                                         <Value Type='Text'>{0}</Value>
                                                      </Eq>
                                                 </Where>", accountName);
                SPListItemCollection itemCollection = spList.GetItems(spQuery);
                if (itemCollection.Count > 0)
                {
                    spWeb.AllowUnsafeUpdates = true;
                    SPListItem item = itemCollection[0];
                    item["MicrosoftTechnology_MyFavoriteDocuments "] = myFavoriteDocuments;
                    item.Update();
                    spWeb.AllowUnsafeUpdates = false;
                }
            });
            }
            catch { throw; }
        }
    }
}