Sunday, 25 May 2014

SharePoint 2010: How do I populate a lookup field in C#?

I had the requirement to populate seeddata in a simple list structure that contained some lookup columns. The lookup requires the identifier/display text of the source object - in my case it was a ParentItem that was being used in the ChildItem table. I created a lookup column 'Parent' and added it the child list.

The following code resolved the problem.
Please note that this code is for seed data that has expected values and does no validation checking or use any defensive programming.

// Add items to the parent item
SPList parentList = web.Lists["ParentItem"];
SPListItem item = parentList.Items.Add();
item["Title"] = "Parent Header 1";

// Add the child item
SPList childList = web.Lists["ChildItem"];
item = childList.Items.Add();

SPListItem parentItem = GetHeaderItem(parentList, "Parent Header 1");

item["Title"] = "My Child Item";
item["Parent"] = new SPFieldLookupValue(parentItem.ID, parentItem.Title);

private SPListItem GetParentItem(SPList list, string title)
SPQuery query = new SPQuery()
Query = string.Format("<Where> <Eq> <FieldRef Name=\"Title\"></FieldRef> <Value Type=\"Text\">{0}</Value> </Eq> </Where>", title)

SPListItemCollection items = list.GetItems(query);
if (items.Count > 0)
return items[0];
return null;

Tuesday, 13 May 2014

SharePoint 2007: .asmx was not recognized as a known document type.

I am interfacing with a SharePoint 2007 farm via web service. I tried adding the _vti_bin/webs.asmx as a service reference when the following error:

The document at the url was not recognized as a known document type.

Fortunately, there is a simple solution to the problem: add ?wsdl to the url and the problem is resolved.

Sunday, 4 May 2014

AngularJS: How do I render HTML in an ng-repeat?

Rendering HTML with Angular should be a simple task - ng-bind-html should do the trick. (see here).

However, I was struggling to do the same thing in an 'ng-repeat'. I tried using $sanitize, but that did not work. Eventually, I found the solution on stackoverflow: instead of having the iteration sitting above the rendering, one can consolidate the two to produce the required result.

                <span ng-repeat="item in items" me-update-flexslider="y">
   <span class="ng-bind-html:{{item}}"></span>

                <span ng:repeat="item in items" ng-bind-html="item"></span>