Saturday, January 31, 2009

The assembly does not allow partially trusted callers

I was creating a Custom page (code in separate code behind file) for sharepoint. I had to deploy both the .aspx file and .dll file. I deployed the .aspx file and the web.config in a separate folder under layouts folder. The .dll file was deployed to the bin folder of the web application. But while trying to see the page, it was showing the following error message.

"The assembly does not allow partially trusted callers"

After some investigation i found that the error will be happening when we try to call some trusted assemblies from not trusted or partially trusted assemblies.
I was able to resolve the problem by doing the following steps.

  1. Signing the assembly with "Mark assemblies with AllowPartiallyTrustedCallerAttribute(APTCA)" option checked in website project properties-->MSBuild Options
  2. Deployed the dll in to the GAC
  3. Added the assembly in the safe control list of the web.config of SharePoint site.

OpenXML a replacement for serverside automation

As per one of our project requirement we had to do lot of word automation. Since the project is based on SharePoint we had to do all these automation in the code behind of custom.aspx page. But later we found that there are some challenges while invoking the word instance in server using a logged in user. We were able to overcome this problem by impersonating a local user. However we found that server side automation is not supported by Microsoft because of the reasons mentioned in the article(http://support.microsoft.com/kb/257757).

They are suggesting the OpenXML approach for word automation. This seems to be a feasible approach compared to the server automation. To start with OpenXML we should understand the structure of it. We can do it easily by renaming a MSWord2007 .docx format file to .zip extension.

Steps

  1. Create a new MSWord2007 document with the name OpenXMLStructure.docx
  2. Rename the file to OpenXMLStructure.zip
  3. Open the archive file using archiving tool.
  4. It will show the entire structure of the MSWord2007 file in OpenXML format.
  5. To have better understanding about each XML file refer the following MSDN link.http://msdn.microsoft.com/en-us/library/aa338205.aspx
  6. Main part is the word.xml file containing the text of the document. we can read the documentXML in a XML DOM object and do all the automation. The following sites are having lot of information about various automation techniques http://msdn.microsoft.com/en-us/library/aa982683.aspx, http://openxmldeveloper.org/archive/category/1007.aspx

Saving a file only with extension

I was trying to create an OpenXML package manually. For that, a file named ".rel" (only with .rel extension) needs to be created. But if generally if we try to rename a file only with extension we will be getting the message "You must type a file name" and we can't rename the file.

There is a workaround to get rid of this problem. Instead of renaming the file, open the file in NotePad. Use the "Save as" option and type only the extension name in the File Name box. It will be saved only with the extension.

Tuesday, December 23, 2008

"Table of Contents" webpart is missing

I was trying to add a "Table of Contents" Webpart to one of my SharePoint page during customization. But i couldn't find it in the web part gallery. I remember i used this webpart in one of my previous project with out any specific steps.

I tried different things to get the webpart in a trial and error manner. At last i was able to get this webpart after activating the "Publishing features".

In my previous project i activated the "Publishing features" for some other purpose, which also added the TOC webpart. So, when i tried to add the TOC webpart it was available right there. I didn't realise it is part of "Publishing features".

Wednesday, December 17, 2008

Persistence in SharePoint workflow

While creating the long running windows workflows it is mandatory to persist the workflow state at some point of time.
  • to release the memory taken by workflow for execution
  • to restore its state during the hardware failure
  • to maintain the scalability of system

So, it is best practice to implement SqlWorkflowPersistenceService and using that we can persist the data in SQL database. This is the out of the box class provided by Microsoft for using SQL db as data store. But if we are going to use different data store we need to create our own persistence service class by inheriting the WorkflowPersistenceService class. Here is a good example given in this blog(http://www.devx.com/dotnet/Article/32247/0) to implement persistence using SqlWorkflowPersistenceService

Well , but what is happening in SharePoint workflows is entirely different process. We will not be having access to the workflow host application like windows workflows. Here SharePoint is hosting the workflow and the persistence service is implemented in it using SqlWorkflowPersistenceService. So, when a SharePoint workflow's controls goes in to some event based activities like "OnTaskChanged" or "OnTaskDeleted" activity, the workflow's state is dehydrated or persisted automatically in to the ContentDB of the particular SharePoint web application.

We can't implement our own persistence logic here because it should be written in the workflow host application rather than the workflow application. While developing SharePoint workflows we will not be having our own separate workflow host application because SharePoint is actually the host and controls the workflow execution. It is still using the workflow runtime to execute the workflows. But it is customized lot to support MOSS with lot of additional features. Persistence is one among those features.

Persistence in MOSS is implemented by SPWinOePersistenceService which is actually inherited from SqlWorkflowPersistenceService. It stores the workflow state in the Workflow table of the ContentDB.


The following picture shows that the workflow's instance state are actually stored in the "Instancedatasize" column of the table.


Once the workflow is completed the instance data is actually released from the database.