Monday, June 27, 2005

MCMS Resource Stream

At some point of time, we may need to access MCMS resource through code. Getting the stream of the resource may be very much useful, if we are trying to manipulate an image resource to do some trick with .NET.

Before analyzing how to get the resource, it is a must to understand MCMS Resources first. MCMS Resources can be divided into two categories depending on how they are added to the system / used by the system.

The first type is commonly referred as 'Resources' in MCMS. They are added to the system through Site Manager / Web Author / MCMS API as resource into the system and with posting these resources can be referred. These resources are named as Shared Resources / Resources. They can be accessed using MCMS API.

The other type of resource come in when adding a local resource with a posting / using MCMS API (CmsContext.AcceptBinaryFile) - a local resource can be added to the system, which can be only referred by that posting / using the URL. It is not possible to access this type of resources through MCMS API. These types of resources are called as Local Resources / Internal Resources.

URL - Shared Resource - "/NR/rdonlyres/GUID of the Resource/an integer/Name of the Resource.File Type"
URL - Internal Resource - "/NR/rdonlyres/GUID of the Positng/BlobId/Name of the Resource.File Type"

So accessing a Shared Resource is simple as accessing a posting. By using the Publishing API, it is possible to get the resource. As we have the GUID of the resource, we can use that to get the resource.


//Here GUID is the guid of the Shared Resource
Resource r = CmsContext.GetByGuid("{"+GUID+"}") as Resource;
Stream stream = r.OpenStream;

But, as internal resources are related with postings (the GUID which relates with internal resource is the Guid of the posting, not the resource - they don't have public Guids) and not publicly available through MCMS API it is not straight forward as Shared Resource to access them. So we may need to use some other common method to access internal resources.

(Don't forget to add references to System.IO and System.Net)

WebClient webClient=new WebClient();
NetworkCredential networkCredential = new NetworkCredential("username", "password","domain");
webClient.Credentials = networkCredential;
Stream outputStream = webClient.OpenRead("URL of the resource");


Blogger Kaj Bonfils said...


I keep getting a 401 (Unauthorized) even though i authenticate as the CMSAdmin user...

Its probably something very simple i've missed.

Any clues?

Regards, Kaj

4:14 PM  

Post a Comment

<< Home