This script updates the PictureUrl user profile property of a user profile in SharePoint 2010.
The highlight is the compilation and execution of a c# inline script in powershell. This approach can be used in many other situations

$cp = new-object codedom.compiler.compilerparameters
$cp.ReferencedAssemblies.Add('system.dll') > $null
$cp.ReferencedAssemblies.Add('system.core.dll') > $null
$cp.ReferencedAssemblies.Add('c:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint\14.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.dll') > $null
$cp.ReferencedAssemblies.Add('C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll') > $null
$cp.ReferencedAssemblies.Add('C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.UserProfiles.dll') > $null
$cp.ReferencedAssemblies.Add('C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.dll') > $null

$script = @'
using System;
using System.Linq;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace OneOff
{

        public class UserProfileIteratorInstruction
    {

        public string Url = "url here";
                
        public  void Run()
        {
            using (var site = new SPSite(Url))
            using (var web = site.OpenWeb())
            {

                var ctx = SPServiceContext.GetContext(site);

                UserProfileManager manager = null;

                try
                {
                    manager = new UserProfileManager(ctx);

                    //--------------------------------------------------------
                    var prof = manager.GetUserProfile(@"user name here");
                    
                    prof["PictureURL"].Value = "value here";
                    NotifyInformation(prof["PictureURL"].Value.ToString());
                    prof.Commit();
                    //--------------------------------------------------------
        
                }
                catch (Exception ex)
                {
                    NotifyException(ex);
                }
            }
        }
        
        public void NotifyException(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        private void NotifyInformation(string p)
        {
            Console.WriteLine(p);
        }
    }

   
}

'@ 


add-type $script -lang csharpversion3 -compilerparam $cp

$b = new-object OneOff.UserProfileIteratorInstruction
$b.Run()

Last edited Mar 2, 2012 at 6:08 PM by xsolon, version 4

Comments

No comments yet.