Wednesday, January 20, 2010

Newline breaks in XSLT (SharePoint DataForm Web Part)

Scenario: In a custom sharepoint .aspx page, I placed a dataform web part for display page of an item. The item has a column called "Contents". This is a field contains data as paragraphs. When I created a new item, entered data into "Contents" field with spaces between paragraphs. I want to see the data as I entered earlier in the EditForm.aspx & DisplayForm.aspx pages.

When I used , the contents field showed the data without newlines between the paragraphs.

Example:

I entered the data in NewForm.aspx as below:
"
aasdflasdf asdf asdf asdf asdf asdf.

234234aefase5asfgasdfa
asdfasdf
asdf
asdf
a

asdfasdfas"

Note: You can see spaces between the above statements. When I tried to display above content in DisplayForm.aspx, it showed without spaces & special characters as below:

"aasdflasdf asdf asdf asdf asdf asdf.234234aefase5asfgasdfaasdfasdfasdfasdfaasdfasdfas"

Solution:

Add following attributes to xsl:value-of tag:
a. select="ddwrt:AutoNewLine(string(@Contents))"
b. disable-output-escaping="yes"

Note:
1. You can find information about ddwrt:AutoNewLine at Microsoft Site.
2. disable-output-escaping="yes" attribute is meant for showing all special characters like quotes and so on.

Friday, January 15, 2010

Cannot add attachment to custom list form in SharePoint 2007

The problem is that whenever you customize a list form—for example, remove assignment and status fields from a form to submit new issues—you break the ability for the user to add attachments to the list item from that form. In most cases, we got an "Error on Page" at in the status bar with the following message shown in the Error Details:

Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.

We finally have a resolution now, although it does take code modifications to each customized list form. See the complete details here:

http://support.microsoft.com/kb/953271

**Pay careful attention to the code. For new item forms, there are SEVERAL changes required to the code and the changes must be made in multiple places.

Source: https://staceybailey.securespsites.com/blog/Lists/Posts/Post.aspx?ID=13

Wednesday, November 11, 2009

Event Receivers: How to add event receivers to a sharepoint list

Scenario: How to add custom event receivers to a specific sharepoint list using SharePoint Object Model

Approach:

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

string assemblyName = "CustomSharePointEventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d517db3ed1131947";

string className = "CustomSharePointEventReceivers.MyEventReceivers";
list.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assemblyName, className);



Note: In general, custom event receivers to a specific list will be added through Sharepoint features by defining a feature event receivers by extending SPFeatureReceiver. In the SPFeatureReceiver, override all the 4 methods i.e, FeatureInstalled,FeatureActivated, FeatureDeactivating and FeatureUninstalling. In these overriden methods, write above code piece.

Event Receivers: Delete all event receivers associated to a list

scenario: How to delete all the event receivers associated to a list?

Approach:


List eventreceivers = new List();

foreach (SPEventReceiverDefinition EventReceiver in list.EventReceivers)
{
eventreceivers.Add(EventReceiver);
}

foreach (SPEventReceiverDefinition er in eventreceivers)
{
er.Delete();
}


Note: We can not have only 2nd foreach loop alone. The current list of event receivers associated to a list gets updated everytime a delete operation is performed. So, an exception will be raised if you use only delete operation.

Change Workflow Task Status from any SharePoint custom component

Scenario: I have a custom workflow running. I want to change status a task from another sharepoint custom component like webpart or so.

Approach:
If we update the workflow task directly by changing the status field value to Completed or to any other status, then usually workflow raises an exception "This task is currently locked by a running workflow and cannot be edited." To get rid of this error, you can use following routine:


private void CompleteTask(string taskTitle)
{
try
{
Hashtable taskHash = new Hashtable();
taskHash[SPBuiltInFieldId.TaskStatus] = "Completed";

string currentuser = SPContext.Current.Web.CurrentUser.Name.ToLower();
Guid siteID = SPContext.Current.Site.ID;
Guid webID = SPContext.Current.Web.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
web.AllowUnsafeUpdates = true;
SPList wfTasksList = web.Lists["WorkflowTasks"];
SPQuery queryWFTasks = new SPQuery();
queryWFTasks.Query = <<>>;
queryWFTasks.ViewAttributes = "Scope=\"RecursiveAll\"";

SPListItemCollection wfTaskItems = wfTasksList.GetItems(queryWFTasks);

if (wfTaskItems.Count != 0)
{
foreach (SPListItem item in wfTaskItems)
{
if (item.Title.ToLower().Equals(taskTitle.ToLower()))
{
SPWorkflowTask.AlterTask(item, taskHash, true);
break;
}
}
}

web.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
// Log the exception here
}
}

SharePoint Workflow : Get From Email Address Configured in Central Admin

Scenario: I want to send email from workflow SendEmail activity. How to get From email address configured in central administration for my sharepoint application.

Note: For every sharepoint application, we can configure out-going email address information in central admin. This can be done in following steps:
a. Go to central admin site -> Application Management
b. Under "SharePoint Web Application Management" section, check for the link "Web application outgoing e-mail settings" and click on it.
c. Select the Web Application for which you want to set the out going email informatino.
d. Fill the following fields:
  • Outbound SMTP server:

  • From address:

  • Reply-to address:

  • Character set:

Approach:
In the SendEmail activity execute code, you write following instruction to get email set in above "From address:" field:

workflowProperties.Site.WebApplication.OutboundMailSenderAddress

How to get SharePoint user information from Person or Group field in SharePoint List

Scenario: I have a SharePoint list with various fields. One of them is Person field, in which I am storing the value selected through People Picker. How to get the Person value from this field through SharePoint Object Model.

Approach:


// Step1: Get SharePoint Item object
SPListItem Item = SPContext.Current.Web.Lists["MyList"].Item[0];

// Step2: Get user field object by passing field display name
SPFieldUser userField = (SPFieldUser)Item.Fields.GetField("Created By");

// Step3: Get user field value object
// SPBuiltInFieldId.Author refers to Created By field's internal name
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(Item[SPBuiltInFieldId.Author].ToString());

// Step4: Get user object
SPUser userVendor = fieldValue.User;