Saturday, April 19, 2008

Sharepoint: Edit (link to edit item) button in list not redirecting to the correct view after update

When you add a Edit (link to edit item) button in a list on a view that is not the default view, you'll notice that after clicking on the button and editing the properties, you will not be redirected to the view you were working on. It will redirect you to the default view of the list.

This is because there is not a "Source" querystring in the link of the button. The link on the button is:

http://servername/Test%20Doc%20Lib/Forms/EditForm.aspx?ID=1

but it should be:

http://servername/Test%20Doc%20Lib/Forms/EditForm.aspx?ID=1&Source=http://servername/Test%20Doc%20Lib/TestView.aspx (a Source querystring that holds the url of the view you are working on)

So I took al look at how this button is built. I found out that there is a Field definition for this button, looking like this:

<Field
ID="{503f1caa-358e-4918-9094-4a2cdc4bc034}"
ReadOnly="TRUE"
Type="Computed"
Name="Edit"
Sortable="FALSE"
Filterable="FALSE"
DisplayName="Edit"
ClassInfo="Icon"
AuthoringInfo="(link to edit item)"
SourceID="
http://schemas.microsoft.com/sharepoint/v3"
StaticName="Edit"
FromBaseType="TRUE">
<FieldRefs>
<FieldRef Name="IsCheckedoutToLocal"/>
<FieldRef Name="ServerUrl"/>
<FieldRef Name="CheckedOutUserId"/>
</FieldRefs>
<DisplayPattern>
<IfHasRights>
<RightsChoices>
<RightsGroup PermEditListItems="required"/>
</RightsChoices>
<Then>
<HTML>
<![CDATA[<a href="]]>
</HTML>
<URL Cmd="Edit"/>
<HTML>
<![CDATA[" onclick="STSNavigateWithCheckoutAlert(this.href, ']]>
</HTML>
<IfEqual>
<Expr1>
<Field Name="CheckedOutUserId"/>
</Expr1>
<Expr2/>
<Then>
<ScriptQuote NotAddingQuote="TRUE">
<ListProperty Select="ForceCheckout"/>
</ScriptQuote>
</Then>
<Else>
<HTML><![CDATA[0]]></HTML>
</Else>
</IfEqual>
<HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<Field Name="IsCheckedoutToLocal"/>
</ScriptQuote>
<HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<Field Name="ServerUrl"/>
</ScriptQuote><HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<HttpVDir/>
</ScriptQuote>
<HTML><![CDATA[') ;return false;" target="_self">]]></HTML>
<HTML><![CDATA[<img border="0" alt="]]></HTML>
<HTML>Edit Document Properties</HTML>
<HTML><![CDATA[" src="/_layouts/images/RTESRCED.GIF">]]></HTML>
<HTML><![CDATA[</a>]]></HTML>
</Then>
<Else>
<HTML><![CDATA[&nbsp;]]></HTML>
</Else>
</IfHasRights>
</DisplayPattern>
</Field>

So basically, what I had to do is copy this Field definition, and make a custom Field definition for the "correct Edit button". After some javascripting, this is my resulting Field defnition:

Now you can add this Field definition in the schema.xml (in the Fields section) of your list definition like this.

<Field
ID="{00C8F32E-9400-4d61-8C6E-373A0E78E62B}"
ReadOnly="TRUE"
Type="Computed"
Name="EditPropertiesWithRedirect"
Sortable="FALSE"
Filterable="FALSE"
DisplayName="Edit Properties"
ClassInfo="Icon"
AuthoringInfo="(link to edit item, with redirect to correct view)"
SourceID="{F326CD4A-69CD-4b61-A3F1-1427543CD745}"
StaticName="EditPropertiesWithRedirect"
FromBaseType="TRUE">
<FieldRefs>
<FieldRef Name="IsCheckedoutToLocal"/>
<FieldRef Name="ServerUrl"/>
<FieldRef Name="CheckedOutUserId"/>
</FieldRefs>
<DisplayPattern>
<IfHasRights>
<RightsChoices>
<RightsGroup PermEditListItems="required"/>
</RightsChoices>
<Then>
<HTML>
<![CDATA[<a href="" ]]>
</HTML>

<HTML>
<![CDATA[" onclick="STSNavigateWithCheckoutAlert(this.href, ']]>
</HTML>
<IfEqual>
<Expr1>
<Field Name="CheckedOutUserId"/>
</Expr1>
<Expr2/>
<Then>
<ScriptQuote NotAddingQuote="TRUE">
<ListProperty Select="ForceCheckout"/>
</ScriptQuote>
</Then>
<Else>
<HTML><![CDATA[0]]></HTML>
</Else>
</IfEqual>
<HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<Field Name="IsCheckedoutToLocal"/>
</ScriptQuote>
<HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<Field Name="ServerUrl"/>
</ScriptQuote>
<HTML><![CDATA[',']]></HTML>
<ScriptQuote NotAddingQuote="TRUE">
<HttpVDir/>
</ScriptQuote>

<HTML>
<![CDATA['); javascript:window.location=']]>
</HTML>

<URL Cmd="Edit"/>
<HTML>
<![CDATA[' + '&Source=' + escape(window.location);]]>
</HTML>
<HTML><![CDATA[return false;" target="_self">]]></HTML>
<HTML><![CDATA[<img border="0" alt="]]></HTML>
<HTML>Edit Document Properties</HTML>
<HTML><![CDATA[" src="/_layouts/images/edititem.gif">]]></HTML>
<HTML><![CDATA[</a>]]></HTML>
</Then>
<Else>
<HTML><![CDATA[&nbsp;]]></HTML>
</Else>
</IfHasRights>
</DisplayPattern>
</Field>


The field will appear in the Create View or Modify View page, so you can select it to add the field to your view. Now you'll have an edit button that redirects to the correct view after updating the properties.

7 comments:

Shane Russell said...

This seems to be what I am looking for. However I cannot seem to find the field definition you described.

Which files am I looking for?

Knrs said...

Shane,

Take a look in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DocumentLibrary\DocLib. The schema.xml file of the standard document library is located in this folder. You can find the defnition of the standard list template in the FEATURES folder too.

However, it is not recommended to modify the standard Document library, it's better to create your own custom document library or list - see http://www.sharepointblogs.com/tbaginski/archive/2007/08/16/creating-a-custom-document-library-feature-in-wss-v3-moss-2007.aspx

Good luck!

Unknown said...

Exellent post. Where can I find the schema for columns within an existing document list?

Unknown said...

Is it possible to use the IfHasRights Tag in a list schema within the context of a folder ?

Anonymous said...

Thanks mate.Its really usefull.It saved my lot of time.

Lucius said...

Thank you so much!

pdf digital signature said...

Excellent! Thanks for this - I've been looking at this feature for ages. Followed your instructions and it works a treat!