7

It it possible to access the Web application/Farm level property bag using jQuery/Javscript/ClientContext? If not, then what are the possible alternatives to access Web Application Farm level property bags using jQuery/Javascript

I will appreciate the code example.

5
  • hard to give you code without knowing what you want to do. Simply pushing property bag values to a web service is probably not a good idea (it can expose sensitive data).
    – Steve B
    Jun 18, 2012 at 12:19
  • I want to get only the property value from a Web Application using client script. Security is not important for me.
    – Geek
    Jun 18, 2012 at 12:21
  • I would appreciate any code example of retrieving the Web Application level property using the web service.
    – Geek
    Jun 18, 2012 at 12:22
  • Still unclear about your goal. If you want to access a specific set of items in the bag will lead to a different kind of answer that if you want to return all items in the bag. In the former case, I would create a custom DTO class with your very specific data and a custom wcf service to return this data. In the later, if you are ok with the security and privacy impact, I would create a simple OData like service that wrap the property bag.
    – Steve B
    Jun 18, 2012 at 12:36
  • Steve, thank you for your concern. I just to want to access key/value pair in my javascript.
    – Geek
    Jun 18, 2012 at 13:57

4 Answers 4

4
+50

If you are going to get the property bags of your web application or farm OFTEN, you could create a custom HttpHandler to create a dynamic javascript file which will load the properties you want. See my blog post about how to create an httphandler. The idea is partly inspired from loading _spPageContextInfo If you think it is the way to go, I can tell more.

EDIT: I found this question very interesting and I created a simple solution which registers a httphandler. When you add this as script in your site you'll get all web properties. The http handler can be altered to suit different purposes. Details are on my blog.

5
  • + 1 I think it could be a good start as long as I will be available to acheive my goal. Please go ahead I'm open to your suggestion
    – Geek
    Jun 18, 2012 at 13:54
  • I would appreciate the implementation with an example.
    – Geek
    Jun 18, 2012 at 14:00
  • I'll try to find some time as soon as possible :) Jun 18, 2012 at 15:14
  • Here you have a solution for getting web application properties. Just extend further. Jun 19, 2012 at 21:13
  • Ok, I will test and get back to you.
    – Geek
    Jun 21, 2012 at 17:06
2

One thing is for sure that you will not be able to access the WebApplication/Farm scoped property bag from the JavaScript Client Object Model because there are no SPWebApplication or SPFarm objects in it. I think your best bet would be building a custom service as many people here have suggested and then exposing the desired property bag through that service.

1
  • +1 Thank you for your suggestion. If not then I am looking for an alternative solution.
    – Geek
    Jun 18, 2012 at 13:56
2

You can use this SP WebService to do some of the work, the rest you'll have to use AJAX to do.

I'm not that strong in backend dev. but an example could be something like:

C#

public object JSONizedObject;
[WebMethod]  
public static string GetWebAppPropertyBag()
{
    // Logic goes here
    JSONizedObject = new JavascriptSerializer.Serialize(PropertyBag object);
    return JSONizedObject;
} 

Javascript (jQuery)

<script type="text/javascript">
$.ajax({
    url: "MyWebService.asmx/GetWebAppPropertyBag",
    type: "POST",
    contentType: "json/application; utf-8";
    dataType: "jsonp"
    success: function (xData, jqXHR) {  
        //Handle returned data here
    },
    error: function (jqXHR, responseType, responseText) { 
        //Error handling here 
    }
});
</script>

Hope this helps you :)

3
  • So, I will have to deploy a custom web service?
    – Geek
    Jun 18, 2012 at 13:53
  • @SPGeek Yes. This allows you to easily get the propertybag from your codebehind and then send it back to the clientside without having to perform a postback and lose necessary values/info.
    – user2536
    Jun 18, 2012 at 14:03
  • could you please lay down the development steps?
    – Geek
    Jun 18, 2012 at 14:04
2

You can also dynamically generate javascript on the server side and register the function in code... This could be done through a webpart, usercontrol, or application page so it can be used in many situations...

You can build the javascript via a string builder and then register it...

Check out this example on msdn. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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