Showing posts with label Object Model. Show all posts
Showing posts with label Object Model. Show all posts

Thursday, August 13, 2009

Adding a Description for SharePoint Designer Workflow

I have seen this question in many places “How to add description for a SharePoint Designer Workflow?” I had the same requirement; googling does not give me the result to achieve the same :-). See the image of a sample workflow generated by SharePoint Designer.



Unfortunately, SharePoint Designer does not provide an option for adding workflow description. I have looked at the files which is generated by designer, but unsuccessful. See the files generated by a SharePoint Designer Workflow



The finally I decided to write a piece of code using SharePoint Object Model to achieve the same.

private void AddWorkflowDescription(string siteURL, string listName, string workflowName, string workflowDesc)
{
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList _linksList = web.Lists[listName];

foreach (SPWorkflowAssociation _wfAssoc in _linksList.WorkflowAssociations)
{
if (_wfAssoc.Name.ToLower().Equals(workflowName))
{
_wfAssoc.Description = workflowDesc;
_linksList.UpdateWorkflowAssociation(_wfAssoc);
break;
}
}
}
}
}


Feature Activated Code

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string _siteURL = "http://mossserver:4545/sites/Demo/";
string _listName = "Links";
string _workflowName = "workflow 1";
string _workflowDesc = "Workflow Descriptiopn updated by code";
AddWorkflowDescription(_siteURL, _listName, _workflowName, _workflowDesc);
}


I have added this code in my “Feature Activated” event. The following image shows a SharePoint designer workflow with description which is generated by the above code.



Note:
If you just want to run this code only once then create a Console Application / Windows application to execute the above code.

Thursday, July 30, 2009

SharePoint Custom Search using FullTextSqlQuery – Format Hit Highlighted Summary

SharePoint search uses “HitHighlightedSummary “column for highlighting the search text in result summary. Querying the HitHighlightedSummary column using FullTextSqlQuery returns the data that contains hit highlights that are surrounded with tags like <c0>…</c0>

This hit-highlight can be easily customized in OOB search core result web part by customizing the xsl. If it is a custom web part and using FullTextSqlQuery then we need to write piece of code to format hit-highlights.

public static string FormatHitHighlightedSummary(object text, string highlightedTag)
{
return Regex.Replace(Regex.Replace(text.ToString(), @"<c\d>", string.Format("<{0}>", highlightedTag), RegexOptions.IgnoreCase), @"</c\d>", string.Format("</{0}>", highlightedTag), RegexOptions.IgnoreCase);
}


While binding the Highlighted Summary, use the above created method as shown below
<%# FormatHitHighlightedSummary(Eval("HitHighlightedSummary"), "i")%>

I’ve used “Italics - <i>” to Highlight the search text… Use this method to format highlighted summary depends on your requirement.

Friday, January 2, 2009

Programmatically adding web part to a SharePoint page from web part gallery

I had a requirement to add a web part to default.aspx page from Web part gallery. I have used a Feature to do the same. Please find the code below which is written in “Feature Activated” event.



Feature Activated




Add WebPart





Get WebPart XML



Hope the code is straightforward to understand.

Friday, December 5, 2008

Code to remove Web part from a page in SharePoint

Here the code snippet to remove Web Part from Web part zone of default.aspx page.

The SPLimitedWebPartManager provides a limited set of Web Part operations that can be performed in our object model.

Thursday, July 17, 2008

Programmatically uploading site templates to site template gallery

I had a requirement to upload site templates to site collection’s site template gallery using SharePoint object model. See the code snippet below, which will be helpful to upload site templates to site template gallery.



This code uses a configuration file, which has a custom section called “SiteTemplatesSection”. This custom section holds the physical path of each site template.

Friday, July 4, 2008

Programmatically Creating Site Collection in SharePoint

It is possible to create site collections programmatically. The Site Collection creation can be accomplished in the following ways

1) Using SharePoint Object Model
2) Using Central Admin’s Admin Web Service

Creation Site Collection using SharePoint Object Model

See the following code snippet, which uses SharePoint Object Model to create site collection.



Creating Site Collection using Admin Web Service

The Admin Web Service of Central Admin has the following web methods



The following code snippet uses “CreateSite” web method to create a site collection.
After the successful creation of site collection, this web method returns the URL of the newly created site.

Thursday, May 29, 2008

Programmatically adding data to SharePoint List’s Choice column

SharePoint has a column type called “CHOICE”. This column will be useful for presenting one of the field from the following
  • Dropdown List
  • Radio Buttons
  • Check Boxes (To Select Multiple Items)

Consider I’ve a list “Resource List” with following columns

Column Name ------ Type
Resource Name --> Single Line of Text
Languages Known --> Choice (C#, VB.NET, Java, C, C++) – Multiselect

Languages Known” is the CHOICE field (Check Boxes), to select multiple languages known by the resource.When adding new items to the Resouce List, notice that multiple items selected for the ‘Languages Known’ CHOICE column will be seperated by semi colan(‘;’).



I had a requirement to add the list item by code. I have used “ChoiceA;ChoiceB;..ChoiceN” for storing the ListItem’s multi-select CHOICE field. After adding ListItem by code, I have Edited the newly added item in the browser, but no check box has been selected for the item which was added by code.

Then after a little effort I found that the multi-select CHOICE data is stored as “;#ChoiceA;#ChoiceB;#...;#
See the below code for adding listitem to Resource List.


Below screen shows the newly added Item in the edit mode