Joel's SharePoint Architect Blog

SharePoint 2013 Training, Architecture, Administration and Development

Subscribe Subscribe  View Joel Jeffery's profile on LinkedIn
joelblogs.co.uk | joelj.co.uk | joeljeffery.co.uk | jfdiphoenix.co.uk

Embedding Resources in Sandbox Solutions

Take One – On Premise SharePoint 2010

When you’re deploying User Code / Sandbox Solutions to the SharePoint 2010 Solution Gallery you may want to include things you’d usually distribute in files under the 14 hive.

One of the chief restrictions in the Sandbox is that you cannot access the file system. One approach is to embed the file as a resource.

To accomplish this, select the file in Visual Studio’s Solution Explorer that you’d like to embed:

Selecting an Embedded Resource

Next, you’ll need a method to retrieve the embedded resource from the compiled assembly (and yes, this works in the Sandbox Smile)

/// <summary>

/// Gets the embedded resource as string.

/// </summary>

/// <param name="filename">The filename.</param>

/// <returns></returns>

private string getEmbeddedResourceAsString(string filename)

{

    string resource = "";

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename))

        using (StreamReader reader = new StreamReader(stream))

            resource = reader.ReadToEnd();

    return resource;

}

The only slightly tricky part is working out the “filename” of the resource inside your assembly. It’s usually in the form: ProjectName.FolderName.Filename.Ext

In my example, I used the following:

string javaScript = getEmbeddedResourceAsString("GoogleMapsWebPart.GoogleMap.GoogleMap.js");

 

Now I can retrieve the contents of the embedded file and use it without needing to deploy any extra resources.

Take Two – SharePoint Online (Office 365) and On Premise SharePoint 2010

The above method fails in the cloud; Office 365 does not like Assembly.GetExecutingAssembly(), so we need an alternative.

First, you need to add a Resource.resx file. You can do this by right-clicking your Visual Studio project node and going to Properties. Then select the Resources tab, and Add Existing File:

Visual Studio Resources: Add Existing File

From there, navigate to your chosen file you’d like to embed.

You need to change your code to use syntax like: <ProjectName>.Properties.Resources.<ResourceName>.

My example looks like this:

string javaScript = GoogleMapsWebPart.Properties.Resources.GoogleMap;

You can now access your resources directly from your code without any Office 365 Sandbox errors.

Technorati Tags: SharePoint, SharePoint 2010, SharePoint Developer

 

You can leave a response, or trackback from your own site.

5 Responses to “Embedding Resources in Sandbox Solutions”

  1. Andrey says:

    It was very simple and useful article. You helped me a lot. Thank you.

  2. Gabriel says:

    Joel,

    Why this article from MSDN says that this is no possible?http://msdn.microsoft.com/en-us/library/gg615454.aspx ??

    Thanks!

  3. superjos says:

    Thanks for this tip. I haven’t tried yet to apply it, but I do think this should be the root cause. By the way, you might want to include additional keywords in this article, so more people might find it. I looked for bits of text from my error, like the following:

    * This solution references prohibited member
    * GetManifestResourceStream cannot be used on this site collection
    * Sharepoint online GetManifestResourceStream

    and found very very few results! … and they did not include this article, anyway.

  4. Kristian Andersson says:

    Hi Joel,
    I have tried your solution without any success. I have created a Resource File (.resx) file and checked that it is as Embedded Resource.

    When i Try to use the reosurce as follows

    lblEdit.Text = My.Resources.Resources.Edit

    I get the following error message:

    “Could not find any resources appropriate for the specified culture or the neutral culture. Make sure “My.Resources.Resources.resources” was correctly embedded or linked into assembly “HR Portal” at compile time, or that all the satellite assemblies required are loadable and fully signed.”

    Please Help me!!!

Leave a Reply