Archive

Posts Tagged ‘client components’

Traversing Taxonomy Terms from branch to leaf

29 January 2013 Leave a comment

I had a need to show the path of terms, starting from a specific term and then showing the path to the final children,

This would involve traversing the structure as the depth is unknown, so I wrote this piece of code to accomplish this using the Client Components.

Please feel free to steal and improve (Only if you give back the improved code though!)

private IEnumerable<Path> Paths(bool includeRoot, Guid termId, string siteUrl)
{
var paths = new List<Path>();
Action<ClientContext, Term, String> traverse = null;

traverse = (context, term, path) =>
{
context.Load(term, t => t.Id, t => t.Name, t => t.Terms);
context.ExecuteQuery();

if (!(term.Id == termId && !includeRoot))
path += path == string.Empty ? term.Name : ” > {0}”.ToFormat(term.Name);

if (term.Terms.Count == 0)
{
paths.Add(new Path
{
TermId = term.Id,
PathText = path
});
}
else
{
foreach (var st in term.Terms)
{
traverse(context, st, path);
}
}
};

using (var context = new ClientContext(siteUrl))
{
var rootTerm = TaxonomySession.GetTaxonomySession(context).GetTerm(termId);

traverse(context, rootTerm, string.Empty);
}

return paths;
}

public class Path
{
public Guid TermId { get; set; }
public string PathText { get; set; }
}

Field in List not available in ListItem using Managed Client Object Model

25 January 2013 Leave a comment

If you have a field called “Folder” it will not be available in the returned ListItem object

e.g. listItem[“Folder”].ToString()

The inner workings of the ListItem object uses an ExpandoObject to store the properties. It seems it mixes this up in the FieldValues Collection with all your custom fields. The Folder property then takes the value from the FieldValues collection to make it available to the Folder property. Thus making your own Folder field “disappear”

Here is how you can replicate it.

  • Create a list based on the Custom List template
  • Add a column called “Folder” and make it a text field
  • Add a column called “DisplayName” and make it a text field
  • Add a column called “MyField” and make it a text field

Add a couple of dummy rows of data

Create a console application in VS that references Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime

Add the following code to the Main method

using (var context = new ClientContext("http://siteurl"))
{
  var query = CamlQuery.CreateAllItemsQuery();
  var listItems = context.Web.Lists.GetByTitle("Testing").GetItems(query);

  context.Load(listItems);
  context.ExecuteQuery();

  foreach (var listItem in listItems)
  {
    Console.WriteLine("Title: {0}", listItem["Title"]);
    Console.WriteLine("Folder: {0}", listItem["Folder"]);
    Console.WriteLine("DisplayName: {0}", listItem["DisplayName"]);
    Console.WriteLine("MyField: {0}", listItem["MyField"]);
  }
}

Here is the code from .Net Reflector that shows the ListItem object populating the properties. It’s also worth noting that the other properties detailed in this method will have the same problem, but it’s unlikely that you’ll call a field FileSystemObject!

protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
    {
        bool flag = base.InitOnePropertyFromJson(peekedName, reader);
        if (!flag)
        {
            switch (peekedName)
            {
                case "AttachmentFiles":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("AttachmentFiles", this.AttachmentFiles, reader);
                    this.AttachmentFiles.FromJson(reader);
                    return flag;

                case "ContentType":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("ContentType", this.ContentType, reader);
                    this.ContentType.FromJson(reader);
                    return flag;

                case "DisplayName":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["DisplayName"] = reader.ReadString();
                    return flag;

                case "EffectiveBasePermissions":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["EffectiveBasePermissions"] = reader.Read<BasePermissions>();
                    return flag;

                case "EffectiveBasePermissionsForUI":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["EffectiveBasePermissionsForUI"] = reader.Read<BasePermissions>();
                    return flag;

                case "FieldValuesAsHtml":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesAsHtml", this.FieldValuesAsHtml, reader);
                    this.FieldValuesAsHtml.FromJson(reader);
                    return flag;

                case "FieldValuesAsText":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesAsText", this.FieldValuesAsText, reader);
                    this.FieldValuesAsText.FromJson(reader);
                    return flag;

                case "FieldValuesForEdit":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesForEdit", this.FieldValuesForEdit, reader);
                    this.FieldValuesForEdit.FromJson(reader);
                    return flag;

                case "File":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("File", this.File, reader);
                    this.File.FromJson(reader);
                    return flag;

                case "FileSystemObjectType":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["FileSystemObjectType"] = reader.ReadEnum<FileSystemObjectType>();
                    return flag;

                case "Folder":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("Folder", this.Folder, reader);
                    this.Folder.FromJson(reader);
                    return flag;

                case "Id":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["Id"] = reader.ReadInt32();
                    return flag;

                case "ParentList":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("ParentList", this.ParentList, reader);
                    this.ParentList.FromJson(reader);
                    return flag;
            }
        }
        return flag;
    }

Here’s a link in TechNet Forums to the initial discussion and a link to the reported bug

Categories: Code Tags:

The tale of two ampersands in a Taxonomy term

14 January 2013 Leave a comment

There are two types of ampersands that you need to be aware of when playing with SharePoint Taxonomy

Our favorite and most loved

& ASCII Number: 38

And the impostor

& ASCII Number: 65286

After reading this article by Nick Hobbs, it became apparent that when you create a term it replaces the 38 ampersand with a 65286 ampersand.

This then becomes a problem if you want to do a comparison with your original source (spreadsheet, database, etc) as they are no longer the same.

As detailed in Nick’s article, you can use the TaxonomyItem.NormalizeName method to create a “Taxonomy” version of your string for comparison.

Below is the code I used in the SharePoint 2013 Client Component which is a little different from the server code.

string myString = “This contains &”;
using (var context = new ClientContext(http://myurl&#8221;))
{
var result = TaxonomyItem.NormalizeName(context, myString);
context.ExecuteQuery();

string normalisedString = result.Value;
}