Thursday, April 22, 2010

How to add Server side code in SharePoint content pages

 
 

As we will see in SharePoint 2003, it was not possible to add a simple piece of server side code in the content pages. But in SharePoint 2007 adding server side code to the content pages is possible. This can be achieved by modifying the web.config file. In the web.config file you can find <PageParserPaths> tag where we need to add some lines of code.

<SharePoint>

    <SafeModeMaxControls="200"CallStack="false"DirectFileDependencies="10"TotalFileDependencies="50"AllowPageLevelTrace="false">

      <PageParserPaths>

        <PageParserPathVirtualPath="/*"CompilationMode="Always"AllowServerSideScript="true"IncludeSubFolders="true" />

      </PageParserPaths>

    </SafeMode>

    .

    .

    .

</SharePoint>

 
 

You can also change the VirtualPath depending upon the requirement just like if you want to add server side code in the content pages for  a specific List/Library, then you can change the VirtualPath as "/List/TestCustomList/*"

Format Input for SharePoint DateTime field

 
 

We can use SharePoint API as well as SharePoint List web reference to update List Items. Using API it is quite straight forward as we can directly assign a DateTime object against a DateTime field in SharePoint list. But with List web reference it is not that straight forward as we need to first convert the DateTime object into the SharePoint DateTime formatted string and then assign it to the specific field.

Provided below are code samples written in C# to format an input as per SharePoint DateTime field.

/* -- Format for Date Only Field -- */      

private
static
String ToSPDate(String strDt)

{

    if (strDt == String.Empty)

        return strDt;

    else

        return (Convert.ToDateTime(strDt)).ToString("yyyy-MM-dd");

}

 
 

/* -- Format for DateTime Field -- */

private
static
String ToSPDateTime(String strDt)

{

    if (strDt == String.Empty)

        return strDt;

    else

        return (Convert.ToDateTime(strDt)).ToString("yyyy-MM-ddTHH:mm:ssZ");

}

 
 

/* -- Build List WebReference CAML String -- */

String strCamlBuilder = "<Method ID='1' Cmd='Update'>";

strCamlBuilder += "<Field Name='ID'>1</Field>";

strCamlBuilder += "<Field Name='DateOnlyField'>" + ToSPDate(strDtInput) + "</Field>";

strCamlBuilder += "<Field Name='DateTimeField'>" + ToSPDateTime(strDtTmInput) + "</Field>";

strCamlBuilder += "</Method>";

Wednesday, April 21, 2010

Adding/Customizing action menu in SharePoint

 
 

A common requirement  that every Sharepoint developer is asked for today is customization of the SharePoint site based on various customer specific requirements. Most often customers have requirements where they want to add some additional features to action menus like Site action menu, ECB menu or List/library Action menu, etc. To achieve this, we have to write custom action in XML and consume it within the site as a new feature. In Sharepoint if we need to integrate new feature for a List / Library, we can achieve this easily by creating a custom action feature. 

  

1. Create a folder named TestCustomAction under C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES directory.

 
 

2. Next, create a file named Feature.xml in the same folder with the following codes.

 
 

<?xml version="1.0" encoding="utf-8" ?>

<Feature Id="0BFF73A7-01ED-41e0-A810-3C764AC5201C" Title="Create new link in action Menu" Description="Add a new link to the action menu of a Custom List." Version="1.0.0.0" Scope="Site" xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest
Location="TestCustomActions.xml" />

</ElementManifests>

</Feature>

 
 

3. Next, create another file, TestCustomActions.xml in the same folder with the following codes

 
 

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction Id="UserInterfaceCustomActions.DocLibActionsToolbar" RegistrationType="List" RegistrationId="100" GroupId="ActionsMenu" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" Title="Generate CSV file for the Custom List">

                                <UrlAction
Url="/_layouts/TestCustomActions.aspx?List={ListId}" />

                </CustomAction>

   </Elements>

 
 

4. Create a page named TestCustomActions.aspx under layouts folder and add your own Business Logic.

 
 

5. Finally, deploy this feature by running the command in the command prompt.

 
 

To activate this feature: stsadm -o installfeature -filename TestCustomAction\feature.xml -url http://TestServer/Site.

 
 

Finally to see the changes in the site do an iisreset.

Starting SharePoint Workflow Programatically

 
 

Most often we are accustomed with the out-of-the-box functionalities in SharePoint to start a workflow attached with a list / library.There might be a requirement where the workflow needs to be started manually from within a programming interface i.e. a custom form which can be used for triggering workflow on specific list / library items.

An example for the above requirement is provided below using VB.Net as the programming language.

Dim list As SPList = web.Lists("ListName")

Dim lstItem As SPListItem = list.GetItemById(itemID)

'obtain an instance of SPWorkflowManager which will be used to start the workflow

Dim manager As SPWorkflowManager = site.WorkflowManager

'get all workflows associated with the list
Dim associationCol As SPWorkflowAssociationCollection = list.WorkflowAssociations
               
'iterate through all the workflow and lookup for the workflow to be started
For Each association As SPWorkflowAssociation In associationCol

      If association.Name = "WorkflowName" Then
             'get workflow association data
             Dim assData As String = association.AssociationData

             'start the workflow
             manager.StartWorkflow(lstItem, association, assData)

             Exit For
      End If

Next