Properly Setting A Managed Metadata Field In CSOM

90% of the bullshit you will read on the net will allude to these messed up delimiters being the solution when trying to set a managed metadata field in Office 365. This is definitely not the way to do it.

[csharp]

string valueTouse = String.Format(“-1;#{0}|{1}”, term.Name, term.Id);

[/csharp]

Not only is this a terrible way to do it through shitty string literals, there are strongly typed means to accomplish the same thing that are better supported and slightly more readable. The appropriate approach looks like the following:

[csharp]

Term item = null;
List list = new List();
foreach (Term term in termset.Terms)
{
context.Load(term);
context.ExecuteQuery();
if ([Do some sort of test here maybe?])
{
// don’t do this ass crazy approach
string valueTouse = String.Format(“-1;#{0}|{1}”, term.Name, term.Id);
//rather add to collection
item = term;
}
}
list.Add(item);
}
field.SetFieldValueByCollection(spListItem, new Collection(list), 1033);
spListItem.Update();
context.ExecuteQuery();

[/csharp]

Where in the above, the spListItem variable is a typed ListItem object. Much cleaner and relevant code. You can get the LCID for the SetFieldValueByCollection automagically if you wanted ala reflection or interrogating the relevant SharePoint proxy objects.

Share

Freeware – SharePoint Field Diff And Copy Utility

Just want to the app?

Download here: http://spfielddiff.codeplex.com/

Recently at a client I was charged with investigating an application that dealt with incoming / outgoing requests. The data that built up the request objects was being stored as SPListItem’s within their respective SPList objects. After investigation, I was directed to keep the very loosely coupled and inefficient data storage scheme that was actually composed of three lists, rather than well designed metadata, the same. Since I was going to be augmenting the lists, it was necessary for me to have a utility that would allow me to source a list, and copy the relevant SPField objects regardless of base type to a destination list. As a result, I ended up writing my own little utility to automate this task.

The application itself is simple, one form, very few controls.

Start the application:

Give it a URL, get the lists:

This is my source SPList, it has a example fields of all the built-in types:

This is my destination SPList, it has a whole lot of nothing:

Doing the diff, obviously there are a lot of differences:

Then if you begin the copy operation, it will mimic the source list:

Have fun. The source is available on the codeplex site.

Share