Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

2011-07-27

Taking off Anchor Tag in Drop Down List

A while ago, I encountered a issue about lookup column in a SharePoint list.
If lookups items are over 20 items, lookup column changes the visual.


Under 20 items:



Over 20 items (You can modify in the drop down list.) : 



I found the solution not to change the visual if lookups are over 20 items in bellow blog post.

Sharepoint lookups over 20 items solution?
(The solution is using DVDropDownList instead of FormField.)


I could resolve my issue referencing the post, but I encountered a new problem. The problem is that a lookup column is extended due to anchor tag in EditForm.aspx.



The resolution is using xlst to take off Anchor Tag.


The code is here. I add xslt code and fix SharePoint:DVDropDownList element.

<td width="400px" valign="top" class="ms-formbody">
  <xsl:variable name="lookup" select="substring-after(substring-before(/dsQueryResponse/Rows/Row/@test, '&lt;/a&gt;'), '&quot;&gt;')" />
  <SharePoint:DVDropDownList runat="server" id="ff4{$Pos}" __designer:bind="{ddwrt:DataBind('u',concat('ff4',$Pos),'SelectedValue','SelectedIndexChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@test')}" selectedvalue="{$lookup}" datasourceid="spdatasource1" datatextfield="Title" datavaluefield="ID" AppendDataBoundItems="True" CausesValidation="True"/>


Also see substring-before function and substring-after function.


2011-07-24

How to Get Workflow History Programmatically

I introduced how to check workflow status in the last post.
Today, I introduce how to get workflow history from Workflow History List. You can find Workflow History List's URL by using SharePoint Designer.



Workflow History List is here. There are some columns associated with workflow.



The code is here. The code enables to get all workflow status associated each item in SharePoint site. You can modify the code to get a specific list's workflow status. I identify workflow history list item using "Workflow History Parent Instance" column and "Primary Item ID" column.(see "if" block.)

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

// This is Console Application //

SPSite site = new SPSite("http://sharepoint-site");
SPWeb web = site.OpenWeb();

foreach (SPList list in web.Lists)
{
    foreach(SPListItem item in list.Items)
    {
        foreach (SPWorkflow workflow in item.Workflows)
        {
            foreach (SPListItem worflowHistoryItem in workflow.HistoryList.Items)
            {
                if (("{" + workflow.InstanceId.ToString() + "}" == worflowHistoryItem["Workflow History Parent Instance"].ToString()) 
                    && (item.ID.ToString() == worflowHistoryItem["Primary Item ID"].ToString()))
                {
                    Console.WriteLine(item.Name + " : " + worflowHistoryItem["Description"]);
                }
            }
        }
    }
}

2011-06-26

How to Check Workflow Status programmatically

Today I'll show checking all workflow statuses in SharePoint site programmatically. Sometimes you may want to check workflow statuses are error or not.

You need workflow status name (list column) when you get a item's workflow status. But you can't get workflow status name from SPWorkflow properties directly. You need to use SPWorkflow.AssociationId property and SPList.WorkflowAssociations property to get workflow status.


The sample code is here.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

// This is Console Application //

SPSite site = new SPSite("http://sharepoint-site");
SPWeb web = site.OpenWeb();

foreach (SPList list in web.Lists)
{
    foreach (SPListItem item in list.Items)
    {
        foreach (SPWorkflow workflow in item.Workflows)
        {
            try
            {
                Console.WriteLine("item url : " + item.Url);
                Console.WriteLine("workflow name : " + list.WorkflowAssociations[workflow.AssociationId].Name);
                Console.WriteLine("workflow status : " + item[list.WorkflowAssociations[workflow.AssociationId].Name]);
                Console.WriteLine();
            }
            catch (ArgumentException)
            {
                //ArgumentException is throwed if the workflow is not exist in the list column. 
                Console.WriteLine("Workflow name : {0} is already not exist in List title : {1}.", 
                    list.WorkflowAssociations[workflow.AssociationId].Name, list.Title);
            }
        }
    }
}

The result is like this :
item url : Lists/Announcement01/1_.000
workflow name : Approval01
workflow status : 2
Workflow status is displayed as a number. The workflow status number are related to SPWorkflowStatus Enumeration. For example, number 2 means "InProgress".

2011-06-17

How to Disable an Expiration Policy (Retention) on a List Using SharePoint Object Model

We usually use Expiration Policy (Retention) in Information Management Policy features. Expiration Policy enables to set expiration date for each item in a list. The item will be deleted when expiration date will come.





If you enable or disable Expiration Policy using SharePoint API, it is little bit complicated.
Expiration Policy including Information Management Policy features is associated list's ContentType. (not list of itself) 


You can see how to enable and create Expiration Policy in bellow blog post.

Using the SharePoint API to Configure an Expiration Policy on a Document Library


If you delete and disable Expiration Policy, please see bellow program code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.RecordsManagement.InformationPolicy;

namespace ConsoleApplication3
{
    class Program
    {
        private const string expirationPolicyFeatureId = "Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration";

        static void Main(string[] args)
        {
            SPSite site = new SPSite("http://sp2010");
            SPWeb web = site.OpenWeb();

            SPList list = web.Lists["Annnounce03"];

            foreach (SPContentType contentType in list.ContentTypes)
            {
                Policy policy = Policy.GetPolicy(contentType);

                if (policy != null)
                {
                    //delete Expiration Policy on the List
                    policy.Items.Delete(expirationPolicyFeatureId);
                }
            }
        }
    }
}

Tips :
- You need to add Reference Microsoft.Office.Policy.dll from /14/ISAPI.
- Expiration Policy is defined as Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.