Friday, November 28, 2008

Changing the AssetPortalBrowser Behaviour

Following are some instructions on how to change the default behaviour of the AssetPickerDialog also known as AssetPortalBrowser.aspx

1) Change the paging size: That's an easy one!
go to the ProgramFiles\....\12\Template\LAYOUTS folder and edit AssetPortalBrowser.aspx. BEWARE. Do not edit with SharePoint Designer. It will change all the paths and nothing works no more.
On that page you will find a control called ObjectList1 of Type SMObjectList. It has a PageSize attribute wich you can change from 15 to anything you like really.

2)Changing the Default Sort behaviour: Tricky!
Sadly enough setting the SortDirection ad SortExpression on the Control will not come into effect as the sort is overridden every time the context changes. And the context changes every time the page is loaded. Thus, no matter what you set here it does not seem to work.
The Workaround includes some C# and visual Studio knowhow.
a) Create a new Class Library
b) Add References to the the Microsoft.SharePoint, Microsoft.SharePoint.Publishing and System.Web dlls
c) Create a new Class that inherits from Microsoft.SharePoint.Publishing.Internal.CodeBehind.AssetPickerDialog
d) override the OnLoad event and set the SortDirection, SortExpression and ExplicitView of the ObjectList1 Control programatically. Do this after calling the base.OnLoad() event to make sure the control is loaded correctly.



public class CustomAssetPicker: Microsoft.SharePoint.Publishing.Internal.CodeBehind.AssetPickerDialog
{ protected override void OnLoad(EventArgs e){
base.OnLoad(e);
this.ObjectList1.SortDirection = "DESC";
this.ObjectList1.SortExpression = "Modified";
this.ObjectList1.CancelSortOperation = false;
this.ObjectList1.ExplicitViewName = "All Documents";
}
}





e) sign the assembly

f)install the assembly into the GAC either using gacutil or drag&drop it into c:\windows\assembly
g)fix up the web.config to allow the new assembly by I)adding it to the SafeControls list and II)the compilation\assemblies list
h)Change the Page directive on AssetPortalBrowser.aspx to inherit from your new class instead of the Codebehind class.

Voailla, you now changed the default sort to always show the last modified files and images first.

16 comments:

bungle said...

Do you have to perform any additional tasks after making such changes? IISreset, for example?

Thanks

Unknown said...

Oops. Sure thing. Every time you do a change to anything in the layouts folder you will need to do an iisreset just to make sure that it clears out the caches. You could simply recycle the application pools, but issreset is simpler.

JC Cristobal said...

Hi

How about browsing thru a list item in a folder on a custom list?

I keep getting error 'list does not exist'..

Is this the default behaviour of the control?

any ideas how to fix this?

JC Cristobal said...

Hi

How about browsing thru a list item in a folder on a custom list?

I keep getting error 'list does not exist'..

Is this the default behaviour of the control?

any ideas how to fix this?

Unknown said...

Hmm. So you want to set the default library to be a folder in a library? That will probably not work.
What Service Pack are you running?

Derek said...

Interesting. I have a situation where I want to still browse images in a single site collection, but just a different one to the one my list is in.

So I need to point the browse button in the AssetImagePicker.aspx to the AssetPortalBrowser.aspx in a different site collection, and then obviously return the full http image path to the imagepicker.

Almost like cross site collection image picking, except that I'm ONLY picking images from the other site collection.

Is that even possible?

Unknown said...

Hmm. Now that is a challenge.
I doubt you will be able to use assets outside of the context of the current Site Collection due to the validity checks it performs upon opening.

In theory it should not make a huge difference to an image Web Part or other link control but keep following in mind:
the site collection needs to be in the same web app to avoid authentication problems.
The user will need access to the site collection.
If you find it works, please post a comment here. Would love to know myself.

Arindam said...

Hi Alex

This is indeed a very good prost, is there a way to show documnet library and Picture libray in this AssetPortalBrowser.aspx?

AC

Unknown said...

You can always browse to a picture library or document library using the asset picker. So I´m not sure if that answers your question.

Ah Lun said...

Great post! Just want to find out is it only me? I found out that this solution seems good for the first view loaded, however, when you clicked on any other shortcuts in 'Look In' (e.g. Current Site: Pages), the sorting reverted to the default. The weird thing is, once you click on the same link again (and caused a post back), the sorting works again if you had set it in PreRender. Seems like setting the ObjectList1's sorting in OnLoad is always overridden? My code attached:

void SetDefaultSorting()
{
this.ObjectList1.SortDirection = "DESC";
this.ObjectList1.SortExpression = "Modified";
this.ObjectList1.CancelSortOperation = false;
this.ObjectList1.ExplicitViewName = "All Documents";
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SetDefaultSorting(); //only works on first load
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

//Checked, at this point this.ObjectList1.SortExpression == "", when other shortcut clicked

//SetDefaultSorting(); //Now, if you included this, sorting will take effect on the next postback. If not, it will always sort using the default (ignoring what we did in OnLoad)
}

Unknown said...

Thanks Ah Lun for your fix. This seems to go hand in hand with the fact that setting the sort expression on the control in aspx does nothing either. Setting the attribute just before rendering will ensure that this is effectively the last statement to set the value before the control gets rendered.

Vishnu Mahesh Adapa said...

Hi in my custom Asset picker dialog i need to have a text box and if i enter some text results matching this text should get displayed in list ..

Is this is possible here..

Unknown said...

Sorry dude, I think you're asking a bit too much there. You can define different views which the picker will display but if you want to search within a site, library or list using the picker you will need to come up with a custom solution. SharePoint has some great webservices for you to use on a custom page that mimics the asset portal picker.

Vishnu Mahesh Adapa said...

hi Alx,

if iam able to get the results from a list based on the text entered. Is there any possibility here to assign back the result to the Grid.

Our requirement is like we will have so many images(around 250) in image librray and user should be able to search for some images matching a string... and selects the image.
is there any documentation related to how the ObjectList1 item is used within the AssetPicker code behind..

beiqirun said...

I followed your steps. But When I Clicked Insert Image Button on html editor(Publishing HTML Field in a List), then I clicked the "Browse" button in the pop-up dialog, then the "AssetPortalBrowser.aspx" got an "Unknown Error" Exception.

web.config:




AssetPortalBrowser.aspx:
<%@ Page Language="C#" Inherits="Augmentum.BaosteelMetal.ImageOrder" MasterPageFile="~/_layouts/dialog.master" %>




Is there anything wrong?

Anonymous said...
This comment has been removed by a blog administrator.