Jul 20, 2011

Tips for Different FQDN and NetBIOSName in SharePoint 2010 User Profiles

SharePoint, SharePoint, and SharePoint again. Today I want to post something about how we can import from Active Directory but with the FQDN and NetBIOSName completely different. If your domain FQDN is (for example) gembelcorpse.co.id, and your NetBIOS Name set to GC, then you do have a problem. When you let this SharePoint User Profile imports from your active directory, it will make the User Profile import like GEMBELCORPSE\administrator, or GEMBELCORPSE\user1, and that’s totally wrong.

In the previous version of SharePoint (2007), we don’t need to do this tips, all things are automatically done by system. Now, in SharePoint 2010, we must do this kind of tips. So, take a look at this PowerShell code.

Code Snippet
  1. param($ServiceApplicationName)
  2. $ServiceApps = Get-SPServiceApplication
  3. $UserProfileServiceApp = ""
  4. foreach ($sa in $ServiceApps)
  5.   {if ($sa.DisplayName -eq $ServiceApplicationName)
  6.     {$UserProfileServiceApp = $sa}
  7.   }
  8. if ($UserProfileServiceApp -eq "")
  9. {
  10.     Write-Host "Ooops..., Service Application '$($ServiceApplicationName)' not found."
  11. }
  12. else
  13. {
  14.     Write-Host "Found '$($UserProfileServiceApp.DisplayName)'..."
  15.     if ($UserProfileServiceApp.NetBIOSDomainNamesEnabled -eq 1)
  16.     {
  17.         Write-Host "NetBIOSDomainNamesEnabled in '$($UserProfileServiceApp.DisplayName)' already enabled, nothings updated!"
  18.     }
  19.     else
  20.     {
  21.         Write-Host "Press any key to continue, [Esc] to cancel..."
  22.         $Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  23.         if ([int]$Key.Character -eq 27)
  24.         {
  25.             Write-Host "Canceling update to '$($UserProfileServiceApp.DisplayName)'..."
  26.         }
  27.         else
  28.         {
  29.             $UserProfileServiceApp.NetBIOSDomainNamesEnabled = 1
  30.             $UserProfileServiceApp.Update()
  31.             Write-Host "Done Updating '$($UserProfileServiceApp.DisplayName)'!"
  32.             Write-Host "Please run Full Synchronization to import all User Profile."
  33.         }
  34.     }
  35. }

With that PowerShell code, run it using SharePoint PowerShell, you can run it from Start menu > Microsoft SharePoint 2010 Products > SharePoint 2010 Management Shell.

image

Run it using –ServiceApplicationName argument. If your User Profile Service Application Name is User Profile SSA, then you should run it like this command below.
.\SP2010EnableNetBIOSName.ps1 -ServiceApplicationName "User Profile SSA"

image

NOTE: You must run this script on a newly created User Profile Service Application. If you already have the existing, you must delete it, or create another User Profile Service Application. Don’t do the Full Synchronization, configure it first (point to active directory), and then run this script, and then run the Full Synchronization of your User Profile Service Application.

Nice….! click +1 if you do like Winking smile

Jul 8, 2011

Get SharePoint User Profile of Specific User Programmatically

Again, I want to share some of my encapsulated methods. So, I’ve been through so many projects, developing this and that, with the same pattern. It very helpful when you choose to use encapsulation and get it for later use.

This method is about get SharePoint user profile. You can use this code whether you are using SharePoint 2007 or SharePoint 2010. Enjoy… Winking smile

Code Snippet
  1. public static UserProfile GetUserProfile(string LoginName)
  2. {
  3.     return GetUserProfile(SPContext.Current.Site, LoginName);
  4. }
  5. public static UserProfile GetUserProfile(SPSite Site, string LoginName)
  6. {
  7.     return GetUserProfile(Site, LoginName, true);
  8. }
  9. public static UserProfile GetUserProfile(SPSite Site, string LoginName, bool RevertSystemAccount)
  10. {
  11.     //check if SharePoint System Account
  12.     if (IsSharePointSystem(LoginName) && RevertSystemAccount)
  13.         LoginName = Site.WebApplication.ApplicationPool.Username;
  14.     SPUser Usr = Site.RootWeb.EnsureUser(LoginName);
  15.     ServerContext SCtx = ServerContext.GetContext(Site);
  16.     UserProfileManager UPM = new UserProfileManager(SCtx);
  17.     if (UPM.UserExists(Usr.LoginName)) { return UPM.GetUserProfile(Usr.LoginName); }
  18.     else return null;
  19. }

Write and Delete Settings to .NET App.Config or Web.Config

I don’t have anything to post Where, but I think this might be useful for you. Want to write and delete appSettings in App.Config or Web.Config? Sure you do know how to read it.

Hope this little code helps

Code Snippet
  1. namespace Rdz.Utility
  2. {
  3.     public static class ConfigUtility
  4.     {
  5.         /// <summary>
  6.         /// Writes to configuration file (App.Config) to specified key, or creates a new Key if doesn't exist
  7.         /// </summary>
  8.         /// <param name="KeyName">The Application Settings KeyName</param>
  9.         /// <param name="Value">The Application Settings Value</param>
  10.         public static void WriteToAppConfig(string KeyName, string Value)
  11.         {
  12.             Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  13.             if (cfg.AppSettings.Settings.AllKeys.Contains(KeyName))
  14.                 cfg.AppSettings.Settings[KeyName].Value = Value;
  15.             else
  16.                 cfg.AppSettings.Settings.Add(KeyName, Value);
  17.             cfg.Save(ConfigurationSaveMode.Modified);
  18.             ConfigurationManager.RefreshSection("appSettings");
  19.         }
  20.  
  21.         /// <summary>
  22.         /// Deletes specified key in app.config
  23.         /// </summary>
  24.         /// <param name="KeyName"></param>
  25.         public static void DeleteAppConfig(string KeyName)
  26.         {
  27.             Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  28.             if (cfg.AppSettings.Settings.AllKeys.Contains(KeyName))
  29.             {
  30.                 cfg.AppSettings.Settings.Remove(KeyName);
  31.                 cfg.Save(ConfigurationSaveMode.Modified);
  32.             }
  33.             ConfigurationManager.RefreshSection("appSettings");
  34.         }
  35.     }
  36. }

Cheers… and happy coding! Hihi...