Daniel Revell
Daniel Revell

Reputation: 8576

Internal name encoding in SharePoint

It's pretty well publicised knowledge that _x0020_ replaces spaces in SharePoint internal field names. When working with SharePoint web services this can be a bit of a nightmare until you write a replace rule when comparing field names.

I've just found myself another one of these "encoding exceptions" in a field called SSL2. Bizarrely the internal name ends out being _x0053_SL2. I've tried something like RRL2 which comes out as _x0052_. From this it appears that the number used in the encoding represents the ASCII value of that character. Space would likewise be _x0020_.

My question is twofold:

  1. In this case, whats causing SharePoint to encode these characters.

  2. Broadly, what sort of encoding is being done and is there a general step I can do to decode strings rather than replacing specific occurances which I've come across and know to be a problem.

Upvotes: 3

Views: 6819

Answers (4)

Brad
Brad

Reputation: 4192

This is an old question, but I wanted to understand the first part of this question and it wasn't answered so here goes...

Essentially SSL2 is being encoded to _x0053_SL2 because it resembles an "A1 mode" excel cell reference ([a-zA-Z]{1,3}[0-9]{1,5}). When Sharepoint sees one of those it decides it needs to encode the first character.

This is obviously not standard XML encoding, and I'm not going to posit any theories as to why this is done, but hopefully this at least explains what the OP observed.

Upvotes: 2

SalientBrain
SalientBrain

Reputation: 2541

It's a secret) :

public static string EncodeName(string name)
{
    var encodedName = string.Empty;
    foreach (var c in name)
    {
        var s = c.ToString();
        byte[] bytes = Encoding.BigEndianUnicode.GetBytes(s);
        //Regex.Match(s, "[^\x00-\x80]")
        //IsCyrillic (0x400–0x4FF) и IsCyrillicSupplement (0x500–0x52F)
        //if (Regex.Match(s, @"\p{IsCyrillic}|\p{IsCyrillicSupplement}").Success)
        if (!Regex.Match(s, @"[a-zA-Z0-9_]").Success)
        {
            encodedName += "_x";
            foreach (var b in bytes)
            {
                encodedName += b.ToString("X2").ToLowerInvariant();
            }
            encodedName += "_";
        }
        else
        {
            encodedName += s;
        }
    }
    if (encodedName.Length > 32)
    {
        encodedName = encodedName.Substring(0, 32);
    }
    return encodedName;
}

Upvotes: 2

Serhiy
Serhiy

Reputation: 4507

You should use System.Xml.XmlConvert.DecodeName

Upvotes: 8

Per Jakobsen
Per Jakobsen

Reputation: 3777

SharePoint XML encodes names because it want to be able to use the field names as attributes in XML.

You can use System.Xml.XmlConvert.EncodeName to encode your names

Upvotes: 0

Related Questions