12

I am creating components and folders with the core service, but when trying to read back the components via WebDav paths i am noticing some characters need to be encoded, while others don't (e.g. "?" does not require encoding, but "." needs to be encoded as %2E ) I want to future proof this, is there a definitive list of characters that need to be encoded when reading WedDav paths?

    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Chinese Characters 相关 Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Acute Characters É Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Plus Symbol + Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Question Mark ? Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Forward Slash / Test")]//Fails
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Percentage % Test")]//Fails
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Period . Test")]//Fails
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Forward Slash %2F Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Percentage %25 Test")]//Passes
    [TestCase(@"/webdav/03C Content Master/Building Blocks/Content/Test Content/PathTests/Period %2E Test")]//Passes

    public void GetWebdavPaths_SpecialChars(string webDavPath)
    {
        OrganizationalItemData folder = null;
        try
        {
            folder = _tridionConnection.CoreService.Read(webDavPath, new ReadOptions {LoadFlags = LoadFlags.Expanded}) as OrganizationalItemData;
        }
        catch {}

        Assert.IsNotNull(folder);
    }

4 Answers 4

5

You can probably try to work with various rules for what needs to be encoded, and indeed some of us would probably enjoy learning about all the exceptions and edge-cases. For practical work, however, you are much better off using Tridion to find the WebDavURL of an item.

If you have the Power Tools loaded, you can simply copy/paste the webdav url from the ItemXml tool. Alternatively, if you can look up the TcmUri of the item in the GUI, you can retrieve the WebDavURL simply via the core service as in the following PowerShell example:

PS C:\Users\Administrator> $core.Read("tcm:5-1062-64",$null).LocationInfo.WebDavURL
/webdav/04%20Web/root/Bananas.tpg

If you are creating the item from the core service, then you probably just need to set the appropriate ReadOptions when calling Create, and you'll be able to retrieve the WebDavURL from the return value.

2
  • Hi Domininc, thanks for the input, I am creating the folder based on a product name retrieved from an external web-service. I need the webdav url to see if it is an existing product or a new one. If its new i create a new folder to contain all the products components (images, product components, PDFs, etc), if not new (aka the folder exists) i update the components in it. So, i think working round the edge cases is the only option, but, as you say, that's the interesting bit :)
    – BikerP
    Commented Jul 22, 2013 at 15:23
  • 1
    In that case, you might also consider "canonicalising" the names in advance. Perhaps you can avoid using problematic characters altogether. Commented Jul 22, 2013 at 18:20
6

You can start by using HttpUtility.URLEncode

Here you have a nice start list of percent encoded characters, although the RFC (3986) is definite.

Also bear in mind that there are a few characters which can't be parsed (the forward slash one) unless you do a whole lot of hacking. Others ( * : % & ) require a fix that Microsoft brought out.

1
  • 1
    thanks for the input, i did look at HttpUtility.UrlEncode and Uri.EscapeDataString (%20 encodes spaces, over + encoding them), neither encode the period symbol, or can work around the forward slash issue. I am thinking the only way to proceed may be to remove periods and slashes and encode the rest. I just concerns me there may be other characters which will present these same issues, but i have not come across them yet in my data set.
    – BikerP
    Commented Jul 22, 2013 at 11:30
4

RFC 1738 Uniform Resource Locators lists characters which are regarded as unsafe in any URL. These must always be escaped. Beyond that, specific schemes may have additional characters that need to be escaped.

A more recent standard: RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax gives more details of various character sets.

You could say that Tridion WebDav URLs are misnamed, as they can be (and often are) used without WebDav being involved at all. Having said that, they were introduced to the product when WebDav support was added (hence the choice of name) so it's reasonable to suppose that any escaping requirements that WebDav has ought to be respected. As WebDav is an extension of HTTP, then an escaping strategy suitable for HTTP is probably pretty close to what you need.

1

I used this in my code:

folderWebDavUrl = Uri.EscapeDataString(folderWebDavUrl).Replace("%2F", "/");
1
  • 2
    if this is supposed to be an answer, maybe you can explain a bit more about it rather than making the answer a one line bit of code? Commented Dec 8, 2015 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.