So I'm working on a web part for a client of mine targeting SharePoint Online, and I've run into a brick wall that I've been banging my head against for the last few hours. The web part executes a query against a list (an Enterprise Wiki library to be exact) and given the result, arranges the items in a tree, grouping the different items by their Wiki Categories field. (With the same items appearing in multiple categories when that's the case) The problem is, when I view this web part on SharePoint Online, I get the error:
Web Part Error: Sandboxed code execution request failed.
Normally, I'd try to debug this on my local SharePoint installation, but for some strange reason, the Taxonomy service isn't currently working, and unfortunately, I don't really have the time to spend hours trying to troubleshoot that, so I decided to try to fly this project blind. (given that it was supposed to be a quick little project anyways)
At the moment, all of the code for the web part is wrapped in a try - catch, as you can see below:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using BD.NAVHierarchy.Util;
using BD.NAVHierarchy.WebParts.NAVHierarchyWebPart.Controls;
namespace BD.NAVHierarchy.WebParts.NAVHierarchyWebPart
{
[ToolboxItem(false)]
[Guid("507fcc5f-b62c-441f-993a-fd7b473f484d")]
public class NAVHierarchyWebPart : WebPart
{
#region Overrides of WebControl
/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
/// <param name="writer"> The <see cref="T:System.Web.UI.HtmlTextWriter" /> object that receives the control content. </param>
protected override void Render(HtmlTextWriter writer)
{
try{
Globals.Ctx = Context;
Tree listTree = new Tree("Pages");
listTree.Populate();
listTree.Render(writer);
} catch(Exception exception){
writer.Write(Content.Error(exception));
}
}
#endregion
}
}
The Content.Error function just takes the Exception, and formats the exception.Source, exception.Message, and exception.StackTrace into a nice error message. I've confirmed that there's no issues with that function, so the problem is definitely somewhere in the web part code. Given that I should be catching any errors, though, I don't see why it's given me the generic error instead.
All the actual work relating to SharePoint is done in the Populate method of the listTree object. (The rest is pretty much just formatting the resulting data nicely, etc)
public void Populate()
{
SPQuery spQuery = new SPQuery{
Query = "<Where><Or><Eq><FieldRef Name=\"ContentType\" /><Value Type=\"Calculated\">Project Page</Value></Eq><Eq><FieldRef Name=\"ContentType\" /><Value Type=\"Calculated\">Enterprise Wiki Page</Value></Eq></Or></Where>",
ViewFields = "<FieldRef Name=\"Title\"/><FieldRef Name=\"FileLeafRef\"/><FieldRef Name=\"FileRef\"/><FieldRef Name=\"Wiki_x0020_Page_x0020_Categories\"/>",
IndividualProperties = true
};
SPListItemCollection items = oList.GetItems(spQuery);
for(int i = 0; i < items.Count; i++){
SPListItem item = items[i];
SPFieldLookup categoriesField = (SPFieldLookup)item.Fields.GetField(Resources.FieldGroupBy);
if(categoriesField == null) throw new Exception("Item #" + item.ID + " did not contain a WikiCategories field.");
object categoryField = item[Resources.FieldGroupBy];
string categoriesText;
if (categoryField == null || S.empty((categoriesText = categoryField.ToString()), true)) {
addItem(item);
continue;
}
string[] categories = categoriesText.Split(';');
foreach(string category in categories){
processCategoryStr(item, category.Trim());
}
}
}
Now, the only thing I can really think of is the fact that I'm attempting to query data from the Wiki_x0020_Page_x0020_Categories field, which is a Managed Metadata field. I've read conflicting information on whether or not you can access this data from a sandboxed solution, given that it's a Lookup field to the Taxonomy service. With that in mind, I tried to just get the string representation of the data stored in that field. (based on the data I could query in javascript) If that is the problem, is that enough to cause it to error out without throwing an exception?