SharePoint 2010 Sandbox serialize & Deserialize the data to JSON

My requirement to serialize strongly typed data (settings) that I will used for later used. But sandbox will not allow us to serialize the data and store into the file. we can't store the file into the file system but we can create the hidden document library(lets say "mydoc" as used in the below example)  in SharePoint we can store over there.

Found the work around

1. JavaScriptSerializer Class its provide the way to serialized the data into JSON.
2. I used this class and serialized the data into JSON.
3. Then I Created the Hidden Document library and store serialized data into in this document library.
4. Whenever I required this setting read the information form that file as string and Deserialize that string into strongly typed data.


Following an example   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using Microsoft.SharePoint; 

 public class MyJSONClass
        {
            public string Property1 { get; set; }
            public string Property2 { get; set; }
        }

 private void button1_Click(object sender, EventArgs e)
        {

            List<MyJSONClass> _JsonSerialized = new List<MyJSONClass>() { 
            
                new MyJSONClass(){ Property1="ABC", Property2="1"},
                new MyJSONClass(){Property1="DEF", Property2="2"},
                new MyJSONClass(){Property1="GHI", Property2="3"}
            };

// Storing Data in document library

            SaveFile(_JsonSerialized);


        }





        private static void SaveFile(List<MyJSONClass> _JsonSerialized)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
// Serialize the data
            string str = js.Serialize(_JsonSerialized);

            using (SPSite _site = new SPSite("http://home"))
            {
                using (SPWeb oWebsite = _site.OpenWeb())
                {
                    SPList list = oWebsite.Lists.TryGetList("mydoc");
                    if (list != null)
                    {
                        oWebsite.AllowUnsafeUpdates = true;
                        string destUrl = string.Format("{0}.txt", "test");
                        byte[] bytes = Encoding.Default.GetBytes(str);
                        SPFile File = list.RootFolder.Files.Add(list.RootFolder.ServerRelativeUrl + "/" + destUrl, bytes, true);

                        list.Update();
                        oWebsite.AllowUnsafeUpdates = false;
                    }

                }
            }
        }



// Get the information form that file

        private static List<MyJSONClass> RetriveFile(int  id)
        {

            JavaScriptSerializer js = new JavaScriptSerializer();

            List<MyJSONClass> JsonDeserialized = new List<MyJSONClass>();
            using (SPSite _site = new SPSite("http://home"))
            {
                using (SPWeb oWebsite = _site.OpenWeb())
                {
                    SPList list = oWebsite.Lists.TryGetList("mydoc");
                    if (list != null)
                    {
                        SPListItem _listCollection = list.GetItemById(id); 

                        oWebsite.AllowUnsafeUpdates = true;
                        SPFile _file = _listCollection.File;
                        byte[] bytes = _file.OpenBinary();

                        string recMsg = System.Text.Encoding.ASCII.GetString(bytes);

//  Deserialize  the data
                        JsonDeserialized = js.Deserialize<List<MyJSONClass>>(recMsg);

                        oWebsite.AllowUnsafeUpdates = false;

                    }

                }
            }
            return JsonDeserialized;
        }

        private void button2_Click(object sender, EventArgs e)
        {
// Read the data back
            List<MyJSONClass> obj = RetriveFile(4);
        }



Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Send Email using SharePoint Rest API

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)