Friday, December 6, 2013

SharePoint 2013 - Claim Information in a Web Part

I ran into a situation where I have to see what is the claims information for the current SharePoint user. You have to have Windows Identity Foundation (WIF) 4.5 installed on the SharePoint server in order to run the code.

Following is the updated version of the code I found on MSDN:

Ref: http://msdn.microsoft.com/en-us/library/ee517271.aspx
using Microsoft.IdentityModel.Claims;

protected override void CreateChildControls()
{
try
  {
      this.Controls.Add(new LiteralControl(GetClaimInfo()));
  }
  catch (System.Exception exp)
  {
       this.Controls.Add(new LiteralControl(exp.Message));
  }
}

public string GetClaimInfo()
{
    String strClaim = null;
    IClaimsPrincipal icp = Page.User as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims
    IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;

            
    // Access claims
    foreach (Claim claim in claimsIdentity.Claims)
    {
     strClaim = claim.Issuer + " - " + claim.ClaimType + " - " + claim.Value + "<br/>" + strClaim;
    }
    return strClaim;
}
Here is another version of the code to get all the AD groups for a user:
public string GetClaimInfo()
        {
            String strClaim = null;
            string strGroup = null;

            IClaimsPrincipal icp = Page.User as IClaimsPrincipal;

            // Access IClaimsIdentity which contains claims
            IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;

            
            // Access claims
            foreach (Claim claim in claimsIdentity.Claims)
            {
                if (claim.ClaimType.EndsWith("groupsid"))
                {
                    strGroup = new System.Security.Principal.SecurityIdentifier(claim.Value).Translate(typeof(System.Security.Principal.NTAccount)).ToString();
                    strClaim = claim.Value + " - " + strGroup + "<br/> " + strClaim;
                }
            }
            return strClaim;
        }

Tuesday, December 3, 2013

PowerShell - Get List Size

Following is the PowerShell script to get the size of a SharePoint list by calculating the size of the attachments for all items in a list:
function GetListSize($List)
{
    [long]$listSize = 0
    $allAttachmentsFolder = $List.RootFolder.SubFolders["Attachments"]

    foreach ($listItem in $List.Items)
    {                   
        $listItemAttachments = $listItem.Attachments
        $attachmentsFolder = $allAttachmentsFolder.SubFolders[$listItem.ID]
        foreach($file in $listItemAttachments) 
        {            
            $listSize += $attachmentsFolder.Files[$file].Length
        }
    }

    $totalInMb = ($listSize/1024)/1024
    $totalInMb = "{0:N2}" -f $totalInMb

    return $totalInMb    
}

Ref: http://sharepoint.stackexchange.com/questions/12652/get-total-size-of-attached-files-on-all-list-items-in-sharepoint-2007with-powers

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...