Monday, April 18, 2011

Display form URL of a list item or folder

The below code provides a generic way to form/build URL to the Display form of a list item or folder. This code makes use of the default dispform specified for the list through the property splist.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url.

using (SPSite oSPSite=new SPSite("< site url > "))
            {
                using (SPWeb oSPWeb=oSPSite.OpenWeb())
                {
                    SPList oSPList = oSPWeb.Lists["ListName"];
                    SPListItem oSPListItem = oSPList.GetItemById(item_id);
                    string displayUrl;
                    string webUrl = oSPWeb.Url;
                    try
                    {
                        displayUrl = oSPListItem.ContentType.DisplayFormUrl;
                        if (string.IsNullOrEmpty(displayUrl))
                        {
                            displayUrl = oSPList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
                        }
                    }
                    catch (NullReferenceException)
                    {
                        displayUrl = oSPList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
                    }
                        
                    bool isLayouts = displayUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
                    displayUrl = String.Format("{0}/{1}?ID={2}", webUrl, displayUrl, oSPListItem.ID);
                    if (isLayouts)
                        displayUrl = String.Format("{0}&List={1}", displayUrl, SPEncode.UrlEncode(oSPList.ID + ""));

                }
            }

Here, the displayUrl gives the link to the dispform of the item.


Saturday, April 16, 2011

Filtered Dataview Web-part using SharePoint Designer

Filtered Dataview web part - Our requirement was to show the related project tasks/issue in the dispform of the project details of the project list, something similar to the Out of the box Project management site template.

Here we are displaying the project tasks under the Project list, filtered by the lookup column "Project" in the Tasks list.

The filtering is done using XSLT using the SelectCommand property.

Steps:
Add the Data View underneath the Projects list form webpart in the dispform.aspx of project list and get the data you want to insert the view of.  Once you have the data view web part there showing all the data in the list (non filtered), switch to Code view.
In the inserted Dataview, just after the tag add the following (If there is already a DataSources tag, then insert only the ....tag.... )


 This is where the data view fetches its data. The "SelectCommand" property of that tag is what applies the filter we want. 
Explanation for the above datasource query is below:
It compares the ID value of the "Project" field in a project task with the current ID of the query string (i.e. currently displayed object).
What makes it work is Type='Integer' and LookupId='True'. This turns the Project value in the Project Tasks list into the equivalent ID. 
The {0} on the next line is the name of the parameter from the query string to compare to. So you'll need another line of code under the SelectParameters (which I can only imagine defines the parameters for data selection) tags.  After the default ListID parameter you should find something like the following:
Code Snippet

< asp:QueryStringParameter Name="0" QueryStringField="ID">
Notice the Name="0" is the same name between the brackets for the SelectCommand. What this basically does is create a parameter (I like to think of them like a variable) with the value of the ID from the query string. 

Tuesday, April 12, 2011

Survey List cannot have workflows

Recently found out that workflows cannot be attached to "Survey" lists in SharePoint. Even though we get to see the option to associate workflows in the List settings page, the workflow doesn't start once a new survey item is posted to the list.
The status of the workflow appears as "Failed to Start".


Refer the below KB article for more details
http://support.microsoft.com/kb/926370