Tuesday, November 25, 2014

Too many characters in character literal

Issue: Getting "Too many characters in character literal" error when try to build a SharePoint project. Error in the following line of code in application page
Assembly Name="$SharePoint.Project.AssemblyFullName$"

Cause: Since the SharePoint project is a sandboxed solution, Sandboxed solutions do not support the application pages.


Resolution: Fixed it by changing the solution from Sandbox to farm.

Thursday, September 4, 2014

Remove duplicate Array from JArray

Following is the code sample that can be used to remove a duplicate array from JArray.

private static string RemoveDuplicateTasks(JArray strArray)

            JArray uniqueArray = new JArray();
            StringBuilder jsonResponse = new StringBuilder();

            //Loop through each array and find for array value with a specific column name. (ex: ID)
            foreach (JObject jObject in strArray)
                //Verify if the ID column value exist in the uniqueArray
                JObject rowObject = uniqueArray.Children<JObject>().FirstOrDefault(o => o["id"] != null && o["id"].ToString() == jObject.Property("id").Value.ToString());

                //rowObject will be null if these is no match for the value in ID column
                if (rowObject == null)

            //Remove the curly braces { }
            int strLength = uniqueArray.ToString().Length;
            string strValue = uniqueArray.ToString().Substring(1, (strLength - 2));
            jsonResponse.Append((jsonResponse.Length > 0 ? "," : "") + strValue);
            return "[" + jsonResponse.ToString() + "]";

Wednesday, July 9, 2014

Get SharePoint Groups added at Web level programmatically

Requirement: To get SharePoint groups added to the specific web site/subsite level. Assumption is that the subsite is not inheriting permissions.


  private static List<string> GetAllWebGroups(string webUrl)
            List<string> lstGroups = new List<string>();

                    using (SPSite site = new SPSite(webUrl))
                        using (SPWeb spWeb = site.OpenWeb())
                            foreach (SPRoleAssignment roleAssignment in spWeb.RoleAssignments)
                                if (roleAssignment.Member is SPGroup)
                                    foreach (SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings)
                                       // To get groups with specific role .
                                       //  SPRoleType.Editor does not work for SharePoint 2010
                                      /*The following condition will not return custom roles, remove this condition in case                                        you want to display all groups.*/
                                        if (roleDefinition.Type == SPRoleType.Reader ||
                                            roleDefinition.Type == SPRoleType.Contributor ||
                                            roleDefinition.Type == SPRoleType.WebDesigner ||
                                            roleDefinition.Type == SPRoleType.Administrator)
                                            if (!lstGroups.Exists(delegate(string sGroupName) { return sGroupName == roleAssignment.Member.Name; }))

Friday, May 16, 2014

Cannot complete this action as the Secure Store Shared Service is not responding. Please contact your administrator.

Using SharePoint 2013;

Issue: We are getting the following error when the portal page is accessed.

"Cannot complete this action as the Secure Store Shared Service is not responding. Please contact your administrator."

Cause: Secure store service is not running or not set with proper permission.

Resolution: In IIS, change the secure store service Application pool identity to a dedicated user if it is pointed to Network service/Local Service.

Because Secure Store is used to store sensitive information, for better security run the Secure Store Service in a separate application pool that is not used for any other service.

Tuesday, May 6, 2014

Error when creating new user group in SharePoint 2010 using Internet Explorer 9/10/11

Environment: SharePoint 2010, IE 9/10/11

Scenario: Create new SharePoint user group



An unexpected error has occurred.
Troubleshoot issues with Microsoft SharePoint Foundation.
Correlation ID: ad4b6182-60df-4a07-998c-c4f11d82c500
Date and Time: 5/6/2014 5:10:53 AM

Resolution: Apply cumulative hot fix

April 2014 Update
SharePoint Foundation 2010

SharePoint Server 2010

Friday, April 4, 2014

Add Sign in as different user menu in SharePoint 2013

Using SharePoint 2013;

This project adds "Sign in as different user" option in SharePoint 2013, which was not enabled out of the box. Build the project and deploy the WSP to SharePoint. The feature to add the menu is scoped at web level. The feature will be activated by default on the site collection.

Click here to download the source code.

Friday, February 21, 2014

Set field as read only on editform/newform during list provisioning

Using SharePoint 2013/2010;

Requirement: Set a Text field 'Application Name' in a list as read only on edit form page load. The list is created during site provisioning.

Solution: Customize the editform of the list by adding a content editor web part and JavaScript to make the field as read only.Add the following code in list's schema.xml file

<Form Type="EditForm" Url="EditForm.aspx" Path="CustomEditForm.aspx" WebPartZoneID="Main" UseDefaultListFormWebPart="False" >
          <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1">
              <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
              <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>                      
          <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="2">
        <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
        <Title>Web part title</Title>
        <Description>Web part description</Description>
        <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
        <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
        <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
       var prev_handler = window.onload;
            this.window.onload = function () {
               if (prev_handler) {

      var lists = document.getElementsByTagName('input');
        for ( var i = 0; i &lt; lists.length; i++) {
              if (lists[i].getAttribute('title') === 'Application Name' ) {
                lists[i].readOnly = true;
            <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />


PS. The script need to be updated based on the field name

Thursday, February 13, 2014

Tuesday, February 4, 2014

The type initializer for 'Microsoft.SharePoint.CoreResource' threw an exception.

Using SharePoint Server 2013;

Error: The type initializer for 'Microsoft.SharePoint.CoreResource' threw an exception.

Scenario: The error was on the line   SPSite site = new SPSite(siteUrl);.
In my VS 2012 project, I was referencing version 14 of Microsoft.SharePoint.dll  and 3.5 as target .net framework version

Resolution: Changed the target framework to 4.0 and referenced the version 15 of Microsoft.SharePoint.dll

The type initializer for 'Microsoft.SharePoint.CoreResource' threw an exception.

Tuesday, January 21, 2014

People and group field is imported as Single line of text field when imported as spreadsheet

Issue: When we create a list using import a spreadsheet and if you have an people and group field in the spreadsheet, after import the people and group field may change as single line of text field.

Workaround: Create a people and group field in the same list where you have imported the list.Following PowerShell script will read the values from single line of text field and update in the value in people and group field. Make sure your AD user profile is similar to the source list/site.

Add-PSSnapin Microsoft.Sharepoint.Powershell
$site = new-object Microsoft.SharePoint.SPSite("SiteURL”)
$web =  Get-SPWeb -Identity siteURL
$lista =$web.Lists["ListName"]
$items = $lista.items
foreach ($item in $items) {
  $user = $item["SingleTextColumnNameOfUserName"]
  write-host $useracc 
$useracc =  $web.EnsureUser( $user); 
$item["NewPeople&GroupFieldName"] = $useracc
write-host $useracc


Wednesday, January 15, 2014

WSP Versioning

Versioning for WSP packages are possible, but we have to do it manually.

·     Embed the version number in the filename of the WSP.
·     Create a text file with the version information inside the package.

Solutions don’t have a version number included by default and it is recommended to add a version number to the solution name. With this approach we know which version of your solution is deployed.
For instance:
Use MyProjectSolution_v5000.10.1560.0.wsp as solution name for the build #1560.

The AssemblyFileVersion is intended to uniquely identify a build of the individual assembly
Used for deployment. You can increase this number for every deployment. It is used by setup programs. Use it to mark assemblies that have the same AssemblyVersion, but are generated from different builds.
In Windows, it can be viewed in the file properties.
// Assembly mscorlib, Version
[assembly: AssemblyFileVersion("5000.10.1560.0")]
[assembly: AssemblyInformationalVersion("5000.10.1560.0")]
[assembly: AssemblyVersion("5000.10.0.0")]

AssemblyFileVersion is informational only.  Incrementing the version does not require a recompile of dependent assemblies.  It helps when browsing your bin folder to know which code version you have installed.
Assembly File Version

Code to read the current Assembly File Version
Assembly thisAssembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(thisAssembly.Location);

PowerShell Command
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Location of your executable").FileVersion

PowerShell script can be used to update the AssemblyFileVersion when the project is build.