Customize custom field displaypattern to pass current item info to another page in MOSS 2007

Suppose we have a custom field and we want to edit it on another page, not in default edit page. Then we need to pass current item information to that page which should be able to access and edit the field.Generally, it is required because we can’t edit item in the listview.CAML is used in the display pattern.

Assume the custom edit page(editme.aspx) is deployed on main site, then first we require host address in display pattern, For this use <HttpHost /> element and after this give page name(editme.aspx).
Output: http://server/editme.aspx
Now we will pass following parameters in query string:
ID: <Column Name="ID" />
fieldName: <Property Select="Name" />
ListGUID: <List/>
PageURL:
<ScriptQuote NotAddingQuote="TRUE">
<PageUrl URLEncode="TRUE"/>
</ScriptQuote>
These information(ID,fieldName,ListGUIDand PageURL) are sufficient to edit this item. The complete code looks like:


<RenderPattern Name="DisplayPattern">
<HTML>
<![CDATA[<a href="javascript: void(0)"
onclick="window.open(']]></HTML>
<HttpHost />
<HTML><![CDATA[/editme.aspx?ID=]]></HTML>
<Column Name="ID" />
<HTML><![CDATA[&fieldName=]]></HTML>
<Property Select="Name" />
<HTML><![CDATA[&listguid=]]></HTML>
<List/>
<HTML><![CDATA[&pageurl=]]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<PageUrl URLEncode="TRUE"/>
</ScriptQuote>

<HTML><![CDATA[')">]]></HTML>
<Column HTMLEncode="TRUE" />
<HTML><![CDATA[</a>]]></HTML>
</RenderPattern>


In editme.aspx page:


string pageurl = Request["pageurl"].ToString();
string fieldName = Request["fieldName "].ToString();
string lstguid = = Request["listguid"].ToString();
int itemID = Convert.ToInt32(Request["ID"]);
using (SPWeb oWebsite = new SPSite(pageurl).OpenWeb())
{
SPList oList = oWebsite.Lists[new Guid(lstguid)];
SPListItem oListItem = oList.GetItemById(itemID);
oListItem[fieldName] = "Sample Value";
oWebsite.AllowUnsafeUpdates = true;
oListItem.Update();
}


On clicking custom field item in listview, editme.aspx page will be opened and value is replaced with “Sample Value”.
Hope ! it helps.

No comments:

Post a Comment