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

Well, subject says it all. Although MSDN has plenty of info what classes are accessible in a sandboxed solution I just couldn't find the info if I can read or write to SPWeb.AllProperties.

Basically I want to create a sandboxed WebPart which is able to read/write a particular setting to the SPWeb property bag.

Would be great if someone could give me a quick pointer as I currently don't have a development VM at my disposal. Thanks!

share|improve this question
    
Just FYI, Sandbox solutions are deprecated in SharePoint 2013. –  Amit Kumawat Mar 12 '13 at 13:15
    
Thanks for the heads up, Amit. but since one of my requirements is to build accessible solutions (no matter if sandboxed or not), client-side JavaScript-based SharePoint 2013 Apps are not an option. –  Sig Weber Mar 12 '13 at 13:24
add comment

2 Answers

up vote 1 down vote accepted

Although SPWeb.AllProperties is not supported in Sandbox, you can read/write to the property bag through the SPWeb.AddProperty, SPWeb.DeleteProperty and SPWeb.GetProperty methods. Yes that's sandbox for you :) See the following link for code on how to update Property Bags with the above methods.

http://community.zevenseas.com/Blogs/Geetanjali/Lists/Posts/Post.aspx?ID=16

share|improve this answer
    
Thanks! I forgot to put my external HDD into my travel bag hence I'll not be able to check that code until I'm back from my road trip, but it sounds like it'll be the solution. Though I'm not sure if the methods write to SPWeb.AllProperties or SPWeb.Propterties. Too bad I can't mark both replies as answer... –  Sig Weber Mar 12 '13 at 12:34
    
Hi there. Your answer is correct. I tested using this code and it works perfectly: web.AddProperty("Tester", "Some Demo Data"); this.Controls.Add(new LiteralControl("<br/>Succesully retrieved Tester from web.GetProperty(\"Tester\") = " + web.GetProperty("Tester").ToString())); –  Fox Mar 13 '13 at 7:54
    
@Sig Weber, please change Vardhaman's answer to the correct one :-) –  Fox Mar 13 '13 at 7:54
    
@Fox, thanks. Done. –  Sig Weber Mar 13 '13 at 10:49
add comment

Ok, so out of the box, you can't. I ran some tests for you and it's not working. I think that you can however achieve it by setting up a full trust proxy: http://sharepointfordeveloper.blogspot.com/2012/06/sharepoint-2010-sandbox-solution-part-4.html

The following code demonstrates the tests:

 protected override void CreateChildControls()
        {
            try
            {
                var web = SPContext.Current.Web;

                string val = web.AllProperties["vti_associateownergroup"].ToString();
                this.Controls.Add(new LiteralControl("Succesully retrieved vti_associateownergroup from web.AllProperties. Value = " + val));
                //Works up until here..   


                //Throws Object Reference Exception
                web.AllProperties.Add("Test", "Tested write on " + System.DateTime.Now.ToString());


                //Throws this exception : Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred.  
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    web.AllProperties.Add("Test", "Tested write on " + System.DateTime.Now.ToString());
                });
                this.Controls.Add(new LiteralControl("Succesfully retrieved Test Key from web.AllProperties[\"Test\"]. Value = " + web.AllProperties["Test"].ToString()));            

            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl("<br/>Exception occurred while trying to read AllProperties. Exception = " + ex.ToString()));
            }
        }
share|improve this answer
    
Thanks for taking the time and running some test code for me, Fox. Much appreciated! The full trust proxy approach sounds like the way to go. Unfortunately, Office 365 doesn't allow a full trust proxy (FTP) farm deployment and I guess other SharePoint hosting companies don't either. Well, well, back to the drawing board. Marked it as answer even tho FTP isn't an option on Office 365. –  Sig Weber Mar 12 '13 at 11:14
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.