Search code examples
tridion

How to get Publication WebDAV URL in C# .NET


I am trying to get my publication WebDAV url and below is the logic which I am trying, I am succesfully getting the webdavurl of page and component, however struggling to get publication url.

public static string getPublicationWebDav(string partialWebdavURL, Package package, Engine engine)
{
string webDav = string.Empty;
string pubURI = string.Empty;
if (!string.IsNullOrEmpty(partialWebdavURL))
{
_log.Info("partialWebdavURL" + partialWebdavURL);

RepositoryLocalObject repLocalObject = null;

if (repLocalObject == null)
{
Item pubItem = package.GetByType(ContentType.Publication);

repLocalObject = (Publication)engine.GetObject(pubItem.GetAsSource().GetValue("ID"));
}
webDav = repLocalObject.WebDavUrl + partialWebdavURL;
}
_log.Info("webDav" + webDav);
return webDav;
}

this line give me error

repLocalObject = (Publication)engine.GetObject(pubItem.GetAsSource().GetValue("ID"));

However when I am trying to get the page object is working fine, below code works fine.

if (package.GetByType(ContentType.Page) != null)
{
Item pageItem = package.GetByType(ContentType.Page);
//_log.Info("pageItem" + pageItem);
repLocalObject = (Page)engine.GetObject(pageItem.GetAsSource().GetValue("ID"));
pubURI = package.GetValue("Page.Publication.ID");
}
else
{
Item component = package.GetByType(ContentType.Component);
repLocalObject = (Component)engine.GetObject(component.GetAsSource().GetValue("ID"));
pubURI = package.GetValue("Component.Publication.ID");
}

My objective is to get Publication webdavURL.


Solution

  • After long search in tridion forums, I have found below solution. Thanks to Nuno!!

    // First we have to find the current object we're rendering

    RepositoryLocalObject context;
        if (p.GetByName(Package.PageName) != null)
            context = e.GetObject(p.GetByName(Package.PageName)) as RepositoryLocalObject;
        else
            context = e.GetObject(p.GetByName(Package.ComponentName)) as RepositoryLocalObject;
    
    Repository contextPublication = context.ContextRepository;
    string webdavUrl = contextPublication.WebDavUrl;
    

    Note that the following code will work on 2011 but not on 2009 (WebDavUrl of repository is not exposed). In 2009 I get the contextPublication.RootFolder.WebDavUrl instead.