Tuesday, April 26, 2011

Related Items webpart

Scenario:
I always wondered why this web part is missing. On the edit form / new form of any item, we always wanted to show things which are connected to a particular entity.

Solution:
Related Items webpart

relateditems

Code:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SKN.SPWebparts
{
[Guid("027a38e8-684a-4f0a-8dde-11c3abaad1cd")]
public class RelatedItems : System.Web.UI.WebControls.WebParts.WebPart
{
private const string const_configuationListName = "Session Docs";
private string m_configuationListName = const_configuationListName;

// Configuration List Name property.
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Custom Properties")]
[WebDisplayName("Configuration List Name")]
[Description("Configuration List Name")]
[DefaultValue(const_configuationListName)]
public string ConfigurationListName
{
get
{
return m_configuationListName;
}

set
{
m_configuationListName = value;
}
}

private const string const_configuationFieldName = "ProjectCode";
private string m_configuationFieldName = const_configuationFieldName;

// Configuration Field Name property.
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Custom Properties")]
[WebDisplayName("Configuration Field Name")]
[Description("Configuration Field Name")]
[DefaultValue(const_configuationFieldName)]
public string ConfigurationFieldName
{
get
{
return m_configuationFieldName;
}

set
{
m_configuationFieldName = value;
}
}


private const string const_configuationAddNewItemLink = "";
private string m_configuationAddNewItemLink = const_configuationAddNewItemLink;

// Configuration Add new item link.
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Custom Properties")]
[WebDisplayName("Add new item link")]
[Description("Set this property to show add new item link.Leave blank for newform.aspx")]
[DefaultValue(const_configuationAddNewItemLink)]
public string ConfigurationAddNewLink
{
get
{
return m_configuationAddNewItemLink;
}

set
{
m_configuationAddNewItemLink = value;
}
}

private const string const_configuationAddNewItemText = "Add new item";
private string m_configuationAddNewItemLinkText = const_configuationAddNewItemText;

// Configuration Add new item link text.
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Custom Properties")]
[WebDisplayName("Add new item link text")]
[Description("Set this property to set the text for the add new item link.")]
[DefaultValue(const_configuationAddNewItemText)]
public string ConfigurationAddNewLinkText
{
get
{
return m_configuationAddNewItemLinkText;
}

set
{
m_configuationAddNewItemLinkText = value;
}
}

const string const_AddNewItemLinkHTML = @"<TABLE border=0 cellSpacing=0 cellPadding=0 width='100%'>
<TBODY>
<TR>
<TD class=ms-partline colSpan=2><IMG alt='' src='/_layouts/images/blank.gif' width=1 height=1></TD></TR>
<TR>
<TD style='PADDING-BOTTOM: 3px;PADDING-TOP: 5px;PADDING-LEFT: 5px' class=ms-addnew><IMG alt='' src='/_layouts/images/rect.gif'> <A id=idHomePageNewEvent class=ms-addnew onclick='javascript:NewItem('{0}', true);javascript:return false;' href='{0}' target=_self>{1}</A> </TD></TR>
<TR>
<TD><IMG alt='' src='/_layouts/images/blank.gif' width=1 height=5></TD>
</TR>
</TBODY>
</TABLE>";

public RelatedItems ()
{

}

protected override void CreateChildControls ()
{
base.CreateChildControls();
}

public override void RenderControl (HtmlTextWriter writer)
{
if (!string.IsNullOrEmpty(ConfigurationListName))
{
ListViewByQuery listviewByQueryWP = new ListViewByQuery();

SPWeb currentweb = SPContext.Current.Web;
SPListItem currentList = SPContext.Current.ListItem;

SPList relatedList = currentweb.Lists[ConfigurationListName];
SPQuery query = new SPQuery(relatedList.DefaultView);

query.Query = string.Format("<where><contains><fieldref name='{0}'><value type='Text'>{1}</value></fieldref></contains></where>", ConfigurationFieldName, currentList.Title);

listviewByQueryWP.List = relatedList;
listviewByQueryWP.Query = query;
EnsureChildControls();
listviewByQueryWP.RenderControl(writer);
RenderChildren(writer);

if (relatedList.DoesUserHavePermissions(SPBasePermissions.EditListItems))
{
if (string.IsNullOrEmpty(ConfigurationAddNewLink))
{
ConfigurationAddNewLink = SPContext.Current.Web.Url + "/" + relatedList.Forms[PAGETYPE.PAGE_NEWFORM].Url;
}
string queryParamValue = string.Format("?{0}={1}&Source={2}", ConfigurationFieldName, currentList.Title, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri));
writer.Write(string.Format(const_AddNewItemLinkHTML, ConfigurationAddNewLink + queryParamValue, ConfigurationAddNewLinkText));
}
}
}
}
}

0 comments: