Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Actually, figured it out. I'll post an answer in a bit. –  juntalis Oct 14 '12 at 18:17
add comment

1 Answer

I ended up finding a way to make this work, so I figured I'd leave an answer here just in case anyone else ever tries doing something similar. After enumerating all the fields on the first item, as well as all the properties on the SPFile of that item, I found that there was a string property there called, "Wiki Page Categories" with the info I needed.

So, instead of doing:

object categoryField = item[Resources.FieldGroupBy];

I replaced the body of my for:

SPListItem item = items[i];
object categoriesField;
string categoriesText;
if (!item.File.Properties.ContainsKey(propName) || (categoriesField = item.File.Properties[propName]) == null || String.IsNullOrEmpty(categoriesText = (string)categoriesField)) {
    addItem(item);
    continue;
}
string[] categoryVals = categoriesText.Split(';');
foreach(string categoryVal in categoryVals) {
    if(!categoryVal.Contains("|")) continue;
    string category = categoryVal.Trim();
    if (category.StartsWith("#")) category = category.Substring(1);
    processCategoryStr(item, category);
}

The string containing the data is stored similar to other SPLookupField values. I ended up with something like the following:

#14;#Navigation|the-category-guid-without-braces;#6;#AnotherCategory|the-other-categorys-guid

Given that information, you now have the label, ID, and unique ID for the taxonomy items your Wiki Page links to.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.