Thursday, 23 May 2013

Disable all event receivers in SharePoint 2010

I found the following page( here ) about disabling global event receivers. A fantastic post that solves a very specific need.

The crux of the solution is as follows:

public class MyClass
{
  SPListItem i = SPContext.Current.Web.Lists["Blah"].Items[0]
  HandleEventFiring handleEventFiring = new HandleEventFiring();
  handleEventFiring.DisableHandleEventFiring();

  try
  {
     i.Update();
  }
  finally
 {
    handleEventFiring.EnableHandleEventFiring();
  }
}

public class HandleEventFiring : SPItemEventReceiver
{
       public void DisableHandleEventFiring()
      {
          this.EventFiringEnabled = false;
       }

       public void EnableHandleEventFiring()
      {
          this.EventFiringEnabled = true;
       }

Cannot login to SharePoint 2010 site created as a Host Header Site Collection

I was creating a development environment for a fellow developer in a new VM. I had gone through my checklist for a new VM:

1. Create the Web Application
2. Create the Host Header Site Collection
3. Add the bindings in IIS
4. Update the hosts file to point 127.0.0.1 to the host header
5. Updated DisableLoopbackCheck ( see here )
6. Give all the users excessive permissions

Still nothing. The site would not render, even though the Farm Solutions where OK and my deployment log has not errors.

I found the solution in the post ( here ) and on the Microsoft site ( here ).

I needed to add a Multi-String registry key named BackConnectionHostNames to the registry at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 and add the host headers.



SharePoint 2010 Client side script to check if a user is in a SharePoint group

The following script looks in the SharePoint object model if the current users exists in the SharePoint group 'My Group Name'. If the user exists, then the button 'My Button' is hidden:

ExecuteOrDelayUntilScriptLoaded(disableControls, "sp.js");

function disableControls() {
    clientContext = new SP.ClientContext.get_current();
    currentUser = clientContext.get_web().get_currentUser();
    clientContext.load(currentUser);
    this.collGroup = clientContext.get_web().get_siteGroups();
    clientContext.load(collGroup);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var groupEnumerator = collGroup.getEnumerator();
    while (groupEnumerator.moveNext()) {
        var oGroup = groupEnumerator.get_current();
        if (oGroup.get_title() == "My Group Name") {
            users = oGroup.get_users();
            clientContext.load(users);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededFoundGroup), Function.createDelegate(this, this.onQueryFailed));
        }
    }
}

function onQuerySucceededFoundGroup(sender, args) {
    if (users.get_count() > 0) {
        UserExistInGroup = false;
        for (var i = 0; i < users.get_count(); i++) {
            if (users.itemAt(i).get_loginName() == this.currentUser.get_loginName()) {
                UserExistInGroup = true;
                break;
            }
        }
        if (UserExistInGroup) {
            $(':submit').each(function () { if ($(this).val() == "My Button") { $(this).hide(); } });
        }
    }
}

function onQueryFailed(sender, args) { }

Sunday, 19 May 2013

Error deploying Nintex workflow: An item with the same key has already been added

My client recently upgraded their version of Nintex Workflow 2010 and suddenly some of my workflows started to fail on deployment.

The following error cropped up:

Error deploying workflow: Server was unable to process request. ---> An item with
the same key has already been added. ---> An item with the same key has already
been added.

WTF????

A little digging unveiled the problem - I had two items in a 'Create Item' shape with the SAME DISPLAYNAME. I needed to move the second item to a new 'Update Item' shape in order for it to work.

I order to get the workflow to deploy, I had to manually remove the 'duplicate' item from the serialized XML. Hooray!!

It was then a simple matter of creating a new shape to update the 'duplicate' value.

Using Nintex 2010 to get a users name from the User Profile Service in Sharepoint 2010

The User Profile Service is powerful, but absolutely useless if you cannot access the information it stores.
The following code and shapes will let you extract this precious information and store it in a Nintex workflow variable.

I was looking to extract the users' name based on their login. Sounds easy, but ended being a little tricky.

The next few steps assume that the User Profile Service is configured and running on your server. (Goes without saying, but I wanted to say it anyway.)

First, call the web service.


and put the data in a variable.

Now the clever bit - xml tags and the appropraite xml namespace around the data so that we can interrogate the data with XPath.

<xml xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService">
{WorkflowVariable:userProfileServiceReturnXML}
</xml>


and finally, write a clever XPath query to extract the required value.

//defaultNS:xml/defaultNS:Values/defaultNS:ValueData/defaultNS:Value

Awesome!




Wednesday, 8 May 2013

Hide My Site and My Profile in Sharepoint 2010 with CSS

My current project is in a shared environment, so disabling services and the like in Central Administration is not an option. However, the following code was able to remove the pesky menu options with a minimum of fuss.

The first attempt was to use a blanket css update:
#mp1_0_0_Anchor,#mp1_0_1_Anchor { display: none; }

but we then found that our menu items in a custom girdview control vanished. Bummer. The solution was narrow the scope of the css in order to be very specific for the menu items.

The following code was produced:

.ms-MenuUIUL li[text='My Site'] { display: none;}
.ms-MenuUIUL li[text='My Profile'] { display: none;}

Check for existance of a file in Sharepoint

I was recently writing some simple code to extract a file from a document library. The code was simple enough, but it was the return value that got me into trouble.

The code is as follows:

try
{
  using (SPSite site = new SPSite(http://blah.com))
  {
    using (SPWeb web = site.RootWeb())
    {
        SPFile file = web.GetFile("my file url");
    }
  }
}

All good so far. However, using my .Net brain is did a 'if (file != null)' .... oops.
The file object is never null.

The correct way to check the result if (if (file.Exists) .....).

Simple.