Friday, November 26, 2010

SharePoint: Diagnose “Error loading and running event receiver” problem

We developer gain experience as we work more and spend more time on learning and applying. Mostly, we only care about the how expertise developers are, in implementing things. But I also emphasis on another dimension of experience: “How expertise developers are in fixing or diagnosing problem”. This diagnosing expertise can mainly be gained with more and more real life working experience. Maybe everyday or every week or every moth, you face a problem and find out the solution by debugging or writing logs or finding solution on the internet. That’s how a problem  is resolved but this may lead you to an experienced person in solving similar problems or at least helping you diagnosing similar problems.

 

Ok, I’m beating around the bush, let me come to the real problem that the post is all about. If you have come around the problem that your event receiver is not running and you found that in log file the error message is something like “Error loading and running event receiver…” then you need to find out what’s wrong with the receiver. As the error message says, the problem might be with loading the event receiver or running the event receiver. You need to find out first if the problem is in loading event receiver. If the problem is in loading event receiver the fix the problem first.  To find out the issue, you can use the following steps:

Step 1: Event Receiver Loading Problem?

You need to find out first if your event receiver is executing or being invoked. The simplest way might be to use logging. If you already have logging capability in your code, you can use the logging. But make sure logging doesn’t have any permission related problem. The easiest way to resolve the issue is to allow user ‘everyone’ to have full control in the file/folder where the log file will be saved. However, revert the permission once you are done with diagnosis.

If your logging works from event receiver, then clearly, the problem is not loading receiver.

 

Step 2: Event Receiver Running Problem?

From step 1 if you find the problem is not loading problem then surely the problem is in running the event receiver. So put proper logs in the event receiver in tracking the problem.

Saturday, November 20, 2010

SharePoint: Event Receiver and Assembly Versions

I had encountered an interesting problem with event receiver few days back. By default we are used to attach an event receiver class to a list by declaring the event receiver in an xml file as shown below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>MyEverReceiverOnAdd</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyProject.MyEventReceiver, Version=2.1.0.0, Culture=neutral, PublicKeyToken=</Assembly>
      <Class>PermissionsEventHandler.PermissionsHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

Figure 1: Event Receiver for version 2.1.0.0

As you can see in the xml file above we need to specify the version of the assembly.

 

Let’s image a scenario:

1. You have deployed your SharePoint solution (which has event receivers) while the assembly version is 2.1.0.0. So when you deployed the SharePoint solution, the list event receivers  in the SharePoint webs are registered with that 2.1.0.0 version of dll.

2. After few months, another version of your product is ready and you have changed the dll version (as this is practice to change your dll version as the development goes on). Now your dll version is 2.2.0.0. So you have updated the event receiver’s definition in xml file to reflect the new dll version as shown below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>MyEverReceiverOnAdd</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyProject.MyEventReceiver, Version=2.2.0.0, Culture=neutral, PublicKeyToken=</Assembly>
      <Class>PermissionsEventHandler.PermissionsHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

Figure 2:Event Receiver for version 2.2.0.0

3. Now if you deploy the latest version of dll in server, the old assembly will be removed from GAC.Now you can get two different behaviors:

  • New site: If you create a new site after version 2.2.0.0 is deployed, there’ll be no problem as the new site will register it’s event receiver with the updated xml (which is using 2.2.0.0.0 as shown in image 2).
  • Old site: Here’s your are in trouble. You attached the event receiver after the version 2.1.0.0 is deployed. So the lists will look for that version of dll in GAC. 

 

Solutions:

So how can you handle the situation? After googling and analyzing I have found three solutions:

  • Fixed Version: One solution might be to keep the event receiver classes in a project whose version will not be changed. So you’ll always refer to the an assembly with the same dll version.
  • Deactivate/Activate Old Features: Another solution might be to uninstall/Deactive and then install/activate the event receiver features for all old sites. But this will add extra overhead in deployment. Additionally, if the number of webs are larger then it’s not viable solution.
  • DotNet Publisher Policy: .net publisher policy can be another option to redirect the reference of old assembly to new one. More information on publisher policy can be found in the links: http://msdn.microsoft.com/en-us/library/dz32563a.aspx, http://support.microsoft.com/kb/891030

Tuesday, November 9, 2010

SharePoint 2010 Error: Cannot make a cache safe URL for "1031/styles/Themable/corev4.css", file not found. Please verify that the file exists under the layouts directory.

I had to take backup from my client’s production site to make some analysis in my local server. So I took backup and restored the site in local server. Restored was successful. Then I changed the site collection administrator of the restored site as it was still pointing to the production server’s site collection administrator. 

 

Then I tried to browse the site and found the interesting error “Cannot make a cache safe URL for "1031/styles/Themable/corev4.css", file not found. Please verify that the file exists under the layouts directory.” The very first thing I noticed the path contains 1031. Anyone can guess that this is language id and thought came to my mind is that my client SharePoint site not in English. The production site was in German language and when I restored the site, its looking for resources in German language ID folder.

Then I download the language pack from MSND and refreshed the browser. It worked like magic….

Sunday, November 7, 2010

SharePoint 2010 Error: System.Web.HttpException: Directory 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\_catalogs\masterpage' does not exist.

I had got the error when I tried to use pagemethods in SharePoint. I had registered the AjaxControlToolkit namespace in masterpage and then I replace ScriptManager with AjaxControlToolkit’s ToolkitScriptManager as shown below:

<ajaxToolkit:ToolkitScriptManager id="ScriptManager" runat="server" EnablePageMethods="true" EnablePartialRendering="true" EnableScriptGlobalization="false"   EnableScriptLocalization="true"/>

The problem was I enabled the pagemethods by setting EnablePageMethos to true. Disabling the pagemethods fixed the issue. However I didn’t explore why there’s the error if pagemethods is enabled.

So disable the pagemethods by setting EnablePageMethods=”false” and then then error will disappear…

SharePoint Restore Error: Your backup is from a different version of Microsoft SharePoint Foundation and cannot be restored to a server running the current version. The backup file should be restored to a server with version '14.0.0.5050' or later.

I had needed to move my dev environment from one Virtual machine to another. So I took backup from source server and tried to restore the backup in new server. However, I have got the error “Your backup is from a different version of Microsoft SharePoint Foundation and cannot be restored to a server running the current version. The backup file should be restored to a server with version '14.0.0.5050' or later.” I was totally confused as I had the same version of SharePoint in both server. Then I double checked the version and it was 14.0.4762.1000 for both servers.

Getting confused I googled and found that solution. This is a bug which is fixed by SharePoint 2010 August cumulative update. The updates are available in the following link:

 

After installing the updated the error gone…

Wednesday, November 3, 2010

SharePoint 2010: Editing an item whose modified version is in pending for approval, causes runtime exception

In SharePoint 2010, You can enable content approval by going to ‘Versioning Settings’ section of list settings page as shown below. The error I’ve found can only be found if we choose “Only Users who can approve items (and the author of the item)” as an option in “Who should see draft items in this list?”.

image

Figure 1: Content Approval settings page

Now let’s have a scenario where three users are involved. The first user, say admin, is in administrator group. There are other two users: contributor1 and contributor2 who belongs to contributor group. Say there’s a list MyList where Content Approval is enabled for the list and only users who can approve the item and the author of the item can see draft items. Now follow the steps:

  1. User contributor1 edits an item. And the draft item is pending for approval by admin user.

  2. User contributor2 visit the item and try to edit the item. When user click edit link the edit dialog comes up but when user click Save button the following error message is shown (if you have enabled stack trace):

image

Figure 2: Exception when try to edit an already pending item

The error message is shown below:

Cannot complete this action.

Please try again.<nativehr>0x80004005</nativehr><nativestack></nativestack>
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Cannot complete this action.

Please try again.<nativehr>0x80004005</nativehr><nativestack></nativestack>

Expected Behavior

The expected behavior would be to disable the save button for item already in pending approval status. Or there should have an settings page where admin can define what to do for items whose draft version is in pending approval stage. The options could be:

  • Hide the item from users who can’t see draft items
  • Show the item as readonly to users who can not see draft items (so edit link could be disabled for users).

 

Conclusion

So if an updated item is waiting for approval and if any other user, except the author and user who can approve, try to edit the item, he/she’ll get the error page shown above. The error is only shown when options are selected for viewing draft items by only author and person who can approve items. Though draft item can be edited/viewed by author and approver, other users can see the old item and can edit. So the error is evident if other users try to edit the item.

Tuesday, November 2, 2010

SharePoint 2010 Runtime Error: “You must fill out all required properties before checking in this document."

In SharePoint 2010, you may find two ways the file upload in a library works based on whether you have custom required fields or not.

File upload in Document Library with no custom required field

By default when a library doesn’t have any required field, after uploading a document, the upload window redirects u to the library view page. So in this case there are two steps as shown below:

image

Figure 1: Two steps file upload process for library with not custom required fields

 

 

File upload in Document Library with custom required field

However, if you add a field as required then after uploading a document you will be shown an intermediate dialog window where you can provide values for required fields. The intermediate dialog is shown below:

image

Figure 2: For library with Required fields, user can see this dialog just after file upload

Now in this window you can enter values for required fields. But what if you don’t enter required field values and just cancel the dialog? In this case the file is checked out by you. You might expect that the dialog should be not canceled without entering required field values. But maybe SharePoint team just wanted to let the user to fill the required fields later on, without forcing to do so in the dialog immediately.

 

 

What if user leave the file checked-out by not entering values for required fields?

Now if user doesn’t fill the required fields on the dialog shown on figure 2 by clicking cancel button, then the file is not checked in and kept checked-out by the user who uploaded the file. Now if you select the ‘document’ item in library you can find the check-in button is enabled (shown in the image below) which means the document is checked-out.

image

Figure 3: File without filling required fields is checked-out

What if you try to check-in the file?

Now if you try to check-in the file by clicking “Check In” (as in figure 3), you will get an runtime exception as shown below:

image

Figure 4: Runtime exception on file check-in

What would be the expected behavior?

So what would be the expected behavior if user clicked on “Check In'” shown in figure 3? As I can expect that the window shown on figure 2 would be presented to let user know that the required fields are missing. So if user still cancels the dialog the file would be in checked-out state. If user put values for required fields and save then the file can be checked-in.