SharePoint : Get User’s login name from claims encoded value

There are sometimes scenarios where we have to get user’s login name and we have only claims encoded value (like this i:0#.w|domain\username or i:0#.f|membershipprovider|abc.xyz@comp.com). In SharePoint 2013 / 2010, there is SPClaimProviderManager class available which can decode the claims and provide user’s login name.

I have created a method, which accepts claims encoded value as parameter and returns user’s login name (only if claims are resolved properly).

private string GetUsernameFromClaim(string claimsEncodedUsername)
{
 try
 {
 SPClaimProviderManager mgr = SPClaimProviderManager.Local;
 if (mgr != null)
 {
 if (SPClaimProviderManager.IsEncodedClaim(claimsEncodedUsername))
 {
 // return the normal domain\username without any claims identification data
 return mgr.DecodeClaim(claimsEncodedUsername).Value;
 }
 }
 }
 catch (Exception ex)
 {
 return claimsEncodedUsername; // Or just return the original username.
 }

 // Return the original username value if it couldn't be resolved as a claims username
 return claimsEncodedUsername;
}

Note : Add namespace “Microsoft.SharePoint.Administration.Claims” to use above class.

Issue in SharePoint Folder name with apostrophe ( ` ) and Launch Dialog property of List / Library

In SharePoint list / library items are created for storing the data. Generally user wants a granular structure for maintaining data and for this they creates folder and place their items. Now, if “Launch Dialog” box property is set to “Yes” for this list / library, then details are shown in dialog box. Now if folder name contains apostrophe ( ‘ ) then JavaScript fails in SharePoint and dialog box will not open for all items which are placed inside that folder.

In SharePoint, certain characters are not accepted while creating folder names : ~ ” # % & * : < > ? / \ { | }.
But apostrophe ( ‘ ) is accepted by SharePoint in folder name, and above mentioned issue will applicable only when launch dialog property of list / library is enabled.

If dialog box of items are not opening properly, then look for all parent folders of that item and check whether those folder contains apostrophe ( ‘ ) or not. If yes, then rename that folder name by removing apostrophe.

Read user claims information in SharePoint 2013

In this article, we will see how to read user claims in SharePoint 2013. For this I have prepared one httphandler, which will read the claims information of current logged in user (authentication type windows, forms or third party trusted provider).

using Microsoft.IdentityModel.Claims;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SSONamespace
{
    class SSOAuthenticationExample : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {            
            string requestUrl = HttpContext.Current.Request.Url.ToString();
            IClaimsIdentity ci = (IClaimsIdentity)HttpContext.Current.User.Identity;
            List<Claim> claimCollection = ci.Claims.OfType<Claim>().Select(x => x).ToList();

	/*Uncomment these lines to see all claims entries */
            //string text = string.Empty;
            //foreach (Claim item in ci.Claims)
            //{
            //    text += item.ClaimType + " : " + item.Value + " | ";
            //}

            //System.IO.File.WriteAllText(@"C:\SSOClaims.txt", text);
		
		//fetch information from claims
            string userName = claimCollection.Where(x => x.ClaimType.ToLower().Contains("userid")).Select(y => y.Value).FirstOrDefault();
         }

        public void Dispose() { }

    }
}

Now deploy this code and add below entry in web.config file of web application :

<system.web>
  <httpModules>
    <add name="SSONamespaceModule" type="SSONamespace.SSOAuthenticationExample , SSONamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3af8d8e2f9e8b6c3"  precondition="integratedmode" />
  </httpModules>
</system.web>

Note : Replace PublicKeyToken value from your own project’s PublicKeyToken

Upgrade SharePoint Sandbox Solution

There are certain scenarios where we have to deploy the sandbox solution again with new functionality (like new column in list). Since existing solution already stores crucial data, then retracting the previous version of solution will result in losing of existing data.

Let say, you have sandbox solution which is already deployed on production with version 1.0.0.0. Now you have to made some changes in list schema of the same solution. To do that, follow these steps to make changes in solution so that upgradation works well:
  1. Make changes in your list like add new column and save it.
  2. Open feature which is linked with list.
  3. Click on “Manifest” option. Expand Edit options.
  4. Replace with the below content, change ListName with you list Name and save it. Here I am upgrading the list in which I have added one more column.
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
    <UpgradeActions>
    <VersionRange BeginVersion=”0.0.0.0″ EndVersion=”1.9.9.9″>
    <ApplyElementManifests>
    <ElementManifest Location=”ListName\Elements.xml” />
    </ApplyElementManifests>
    </VersionRange>
    </UpgradeActions>
    </Feature>
  5. Now open properties of same feature (press F4 when you are at feature window). There you will find Version property. Currently it is blank and you have put there any version which lies within 0.0.0.0 to 1.9.9.9
  6. Let say I am putting 1.1.0.0 and save it.
  7. Publish the solution and create wsp package.
  8. Rename the wsp package because existing solution with same name is already present.
  9. Now open Sharepoint management shell with admin account.
  10. Execute below powershell commands to add and upgrade the new solution
    1. Add-SPUserSolution -Site <sitecollectionURL> -LiteralPath <full path of sandbox solution wsp>
    2. Update-SPUserSolution -Identity <existingSolutionName> -Site $workspaceUrl -ToSolution <newSolutionName>
  11. Go to list settings and you will find new column.

Note : Follow above steps in case you have to update content type. Changes will be done at step 4 where you have to use AddContentTypeField instead of ApplyElementManifests

For more details, please follow read below articles: