2014-12-11

Maximum Number of Variables per Activity

This issue has been raised on SharePoint StackExchange.

I am currently developing a Workflow 2013 in VS 2012 as a part of Full trust solution. I added a variable to the workflow and tried to deploy my solution but VS threw an error:
Error occurred in deployment step 'Activate Features': Microsoft.Workflow.Client.ActivityNotFoundException: The activity named 'WorkflowXaml_ed00d3bd_4796_41ba_b288_35ce2226f89a' from scope '/SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143' was not found. HTTP headers received from the server - ActivityId: 56a026d5-261b-443e-bada-30ceb3417263. NodeId: ZG-SP2013-02. Scope: /SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143. Client ActivityId : f86d411a-425b-42ab-8
After fiddling around with this issue for a few hours, I was able to locate more detailed exception message in ULS. ULS records have to have filter Level=Exception:
Workflow XAML failed validation due to the following errors:
Activity 'Sequence' has 51 variables, which exceeds the maximum number of variables per activity (50).
 HTTP headers received from the server - ActivityId: 2bb3ba9e-28d3-4820-91a7-f03ebdb98fdf. NodeId: ZG-SP2013-02. Scope: /SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143. Client ActivityId : e87640d8-8819-4981-bbdb-c46c02eb6d6f.
I counted variables in my workflow and indeed there are 51 variable defined in the workflow. All of the variables are defined in root Sequence. Unfortunately, this is not documented in Workflow limitations web page on MSDN.

Solution:
Reorganize variables in multiple Sequences and reuse existing variables where possible.

2014-10-29

Visual Studio 2013 Cannot Deploy Content Type Updates

After spending 10+ hours trying to figure out why content types included in my wsp cannot be deployed using Visual Studio 2013 deploy without recreating SP site I must say I am disappointed with my findings. Content types cannot be removed from SP site by using Visual Studio 2013 Retract command. My colleague can successfully deploy (and retract) content types using Visual Studio 2012, so I decided to look inside what is really happening under the hood.

Apparently, both Visual Studio versions, 2012 and 2013, are using Microsoft.VisualStudio.SharePoint.Commands.Implementation.V5.dll to deploy and retract SharePoint solutions to SharePoint sites. But the big difference between the two is that VS 2012 implements Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeatures in a way so that it calls SPFeatureCollection.Remove method with force argument set to true which causes a call to stored procedure proc_DeactivateContentTypeInScope with SQL parameter @IsDeactivatingFeature set to 2. When this parameter is set to 2 then stored procedure skips a call to another stored procedure named proc_IsContentTypeInUse. This basically forces the deletion of content type.

VS 2013 implements Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeatures with a call to Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeature method which makes a call to SPFeatureCollection.Remove with force argument set to false which causes a call to stored procedure proc_DeactivateContentTypeInScope with SQL parameter @IsDeactivatingFeature set to 1. This prevents content type from being deleted.

I am using version of VS 2013 which is Visual Studio Premium 2013 Version 12.0.30723.00 Update 3.

I hope MS provides a fix to this issue soon. Until then I am reverting to VS 2012.

2014-09-22

ECB Missing on Manage User Profiles page in Central Administration

Today I was working on SharePoint 2013 dev machine when I was trying to sync AD accounts with SharePoint. I wanted to set emails for accounts to test workflow tasks but I was surprised I couldn't edit user profiles because ECB menus were not rendered on Manage User Profiles (ProfMngr.aspx) page:

Apparently the problem lies in the fact that CA site was browsed with IE 11. To fix this problem, site has to be added to compatibility view list. In Internet Explorer 11 open Tools menu and choose Compatibility View Settings menu item while browsing CA site. The following window should open:

Click the Add button. Window should now look like this:

Close the window and voila, now ECB should be rendered. Notice how grid lines are added to the list:


2014-08-26

Error occurred in deployment step ‘Activate Features': System.TimeoutException: The HTTP request has timed out after 20000 milliseconds.

I am running SP 2013 dev environment and I recently ran into a problem described in the post title. I tried to deploy a solution with feature activation which included SP 2013 Workflow SPIs. I tried to solve the issue by applying a solution described here but it wasn't helpful in my situation. I already had registry settings set up to extend timeout period for SharePoint deployment.

Visual studio output window might look like this:
  Activating feature 'Feature1' ...
Error occurred in deployment step 'Activate Features': System.TimeoutException: The HTTP request has timed out after 20000 milliseconds. ---> System.Net.WebException: The request was aborted: The request was canceled.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at Microsoft.Workflow.Client.HttpGetResponseAsyncResult`1.OnGotResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at Microsoft.Workflow.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.Workflow.Client.Ht


I noticed that I had Fiddler2 running in the background while trying to deploy my solution. I exited Fiddler2 application and tried to deploy again. This time deploy was successful. It very well might be a cause of error. Just to be sure I tried to reproduce the same behavior again by starting Fiddler2 again and trying to deploy the wsp using Visual Studio 2012. This time VS couldn't delete workflows and workflow associations from the site but it was able to deploy successfully. I turned off Fiddler2 again and tried to deploy, and it went smooth, without any errors. I might be wrong but it seems to me that Fiddler's proxy is interfering with deployment of workflows.

I know there are many reasons causing this error but this solution helped me. Hopefully it helps someone else.

2014-08-20

Implementation of ribbon button availability by calling asynchronous methods

It is known that SharePoint ribbon can be extended by implementing CustomAction Ribbon.Library.Actions.AddAButton. SharePoint allows you to dynamically control whether this ribbon button should be enabled. This is done by implementing Javascript logic in CommandUIHandler's EnabledScript attribute.

The problem is if you want to include asynchronous call in aforementioned attribute because this Javascript block must return boolean value. So, when SharePoint calls asynchronous method it will not get the result immediately. Instead, it will get the result of async call in callback function but it's too late, Javascript block has already returned.

The solution to this problem was very well described by Andrew Connell on his blog post. Since no code snippets were included in this post I decided to provide it here because there is one caveat that is not so obvious from first look at the Andrew's post.

This is the implementation of ribbon button in Elements.xml:

    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
                  Command="NewRibbonButtonCommand"
                  Image16by16="/_layouts/15/Style/buttonIcon16.png"
                  Image32by32="/_layouts/15/Style/buttonIcon32.png"
                  LabelText="New case"
                  ToolTipTitle="Get new case."
                  ToolTipDescription="Assigns new case to you."
                  TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="NewRibbonButtonCommand"
          EnabledScript="javascript:IsCurrentUserMemberOfGroup('puk ref');       
       "/>
      </CommandUIHandlers>
    </CommandUIExtension>

Since our project doesn't include customized forms nor master pages we had to include external Javascript file to Elements.xml:

  <CustomAction
   ScriptSrc="/_layouts/15/Scripts/SPRibbonHelperScript.js"
   Location="ScriptLink"
   Sequence="1001">
  </CustomAction>

This makes it easier to debug the solution if Javascript code is included in external file. Prerequisite for this step is to add new Javascript file to layouts mapped folder.

Next piece of code is the actual implementation of enabling/disabling the button based on current user's group membership in SPRibbonHelperScript.js:

var isMemberOfGroup = false;

function IsCurrentUserMemberOfGroup(groupName) {
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();
    var currentUser = currentContext.get_web().get_currentUser();
    currentContext.load(currentUser);

    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);

    var group = allGroups.getByName(groupName);
    currentContext.load(group);

    var groupUsers = group.get_users();
    currentContext.load(groupUsers);
    currentContext.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args) {
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }

        if (isMemberOfGroup == false || isMemberOfGroup == 'undefined') {
            if (userInGroup == true) {
                isMemberOfGroup = userInGroup;
                RefreshCommandUI();
            }
        }
    }

    function OnFailure(sender, args) {
    }

    return isMemberOfGroup;
}