C# String problems

When retrieving HTML or string content from some editors such as SharePoint editor, the editor may insert zero-width or control characters in the content that can cause problem methods like string.IndexOf, string.Compare, string.Replace etc.

If the application only uses English language, it’s possible to strip all control characters from the data before doing other string operations.

Use the Regex to do so

data = Regex.Replace(data, @”[^x20-x7F]”, “”);

This line will remove all characters that are not in the range 0x20 to 0x7F in the ASCII table.

Then proceed to do string operations as per normal.

Read DateTime data from Excel file using C#

Excel files are very tricky to deal with, sometimes it returns double value, sometimes just plain string.

The example processes values read by EPPlus, might be useful in other situations, too.

public DateTime GetDate(object valFromExcel)
{
if(valFromExcel is DateTime) return valFromExcel;
if(valFromExcel is double) return DateTime.FromOADate((double)valFromExcel);
return Convert.ToDateTime(valFromExcel);
}

How to organize Javascript code

Make intensive use of namespaces, don’t use globally declared functions

//if the namespace has already been declared, use the existing one, otherwise create an empty object
//for the namespace
var theNamespace = theNamespace | {};

//assign functions and objects in the namespace by setting properties.
theNamespace.aFunction = function(){};

Using namespace will help avoid functions with same name being overridden by each other.

Deployment stuck at deploying in Sharepoint Solutions Manager

From Central Admin, go to  System Settings > Manage Farm Solutions, then “stop” the deployment of the WSP from there.
Before deploying the solution using central admin, make sure that the sptimer service and sp web services root application pool is running on all servers (WFE and APP)
The reason why it got stuck is that it couldn’t complete the deployment on one or more servers. Make sure that all the servers are accessible and have enough space.

Read SharePoint workflow status in JavaScript

Source: http://sharepoint.stackexchange.com/questions/94648/sharepoint-2013-check-workflow-status-programmatically-javascript

var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var list = web.get_lists().getByTitle(listTitle);
  var listItem = list.getItemById(listItemId);  
  context.load(listItem);
  context.executeQueryAsync(
     function() {
       var workflowStatusField = listItem.get_parentList().get_fields().getByTitle(workflowTitle); //get workflow status field
       var workflowStatusValue = listItem.get_item(workflowTitle); //get workflow status field value
       success(workflowStatusValue);
     },
     error
  );

Secure application pages in Sharepoint applications

For SharePoint applications, some times we have to create ASP.NET Application pages that reside under the _layouts folder and are not managed by SharePoint Security system.

We have to secure them manually by code, one way is to check in Page_Load and disable controls if necessary.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserIsAuthorized)
            {
                //hide the form
                //show an error message
            }
}

Make sure to check if user is authorized in critical methods.

To check if user is authorized:

protected UserIsAuthorized
{
   get
  {
      SPUser  user = SPContext.Current.Web.CurrentUser;

     if(user==null) return false;//anonymous/public users

     //check if user belongs to authorized groups
            string[] validGroups = new string[] { “Group A”, “Group B };
            SPGroupCollection groups = user.Groups;
            foreach (SPGroup group in groups)
                if (validGroups.Contains(group.Name))
                    return true;

    //check if user is System Account
    if (user.ID == SPContext.Current.Web.Site.SystemAccount.ID)
                return true;

    return false;//user is not authorized;
   }
}