CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Tag Archives: serialize

Push a copy of Resources to client in javascript

In one of my previous posts I told about pushing a copy of an object into client. Now I’ll try to copy my resource values into client. The problem is often that we must create multiple localization resources: first as resx-file. If we use much ajax and client side rendering, we must provide some localization there, too. If they are different subsets of localization resources, it isn’t a problem. But when you get overlapping, it becomes more difficult to manage and sync them.

See how Microsoft Ajax Library provides some strings:

What if we just copy the values from resx file into client? If there are not business constraint it can make the development much easier. Let’s try it. For that we need Reflection. We start with changing the access modifiers on resx file to public:

Then we must get all static properties of the auto-generated class (ResXFileCodeGenerator):

var t = typeof(Takana.Localization);
var props = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
const string f = "\"{0}\":\"{1}\"";
var properties = props.Select(p => string.Format(f, p.Name, p.GetValue(t, null))).ToArray();
var entries = string.Join(",", properties);
var json = string.Format("{{{0}}}", entries);
var script = "var takanaLoc = " + json + ";\n";
Page.ClientScript.RegisterStartupScript(GetType(), "takanaLoc1", script, true);

Push a copy of an object to client

To reduce postbacks and database calls, a copy of the current object(s) can be pushed down to the client in JSON format. Say a webpart renders information about a person, another webpart shows related information which is retrieved dynamically (like web services). Instead of getting the current person from the database in the second webpart again, we can reuse the same person object from the first webpart. To do so we must provide a DataContract for the Person class:

[DataContract]
public class Person
{
	[DataMember]
	public string Firstname { get; set; }
	[DataMember]
	public string Lastname { get; set; }
	[DataMember]
	public string PhoneMobile { get; set; }
	...

Then we can serialize the current object using DataContractJsonSerializer:

private void RegisterPersonOnClient(Person person)
{
	var serializer =
		new System.Runtime.Serialization.Json
			.DataContractJsonSerializer(person.GetType());
	var ms = new MemoryStream();
	serializer.WriteObject(ms, person);
	var json = Encoding.UTF8.GetString(ms.ToArray());
	var script = "var personOnClient = " + json + ";\n";
	Page.ClientScript
		.RegisterStartupScript(GetType(), "uniqueScriptKey", script, true);
}

To be able to use these classes, add reference to System.Runtime.Serialization and System.ServiceModel.Web dlls. Don’t forget to append a semicolon and newline so that script doesn’t collide with other scripts.

On the client we get a copy of the person object:

Read more of this post

json serializer in Sharepoint

There is a very handy JSON lib for serializing javascript objects. It is hosted on github under douglas crockford.

Download json2.js and serialize with JSON.stringify function:

EDIT: There is actually this function in core javascript. It exists since javascript 1.7 and ecmascript 5. So you don’t have to add anything to get this functionality:

var t = { name: "dev", city: "new york" };
JSON.stringify(t);

There are actually built-in goodies for serializing javascript objects in ASP.NET: Sys.Serialization.JavaScriptSerializer.serialize

var t = { name: "dev", city: "new york" };
Sys.Serialization.JavaScriptSerializer.serialize(t);


I discovered it when I “studied” the javascript code which follows with every page. Will see if more goodies can be discovered there:

Read more of this post

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

SharePointDiver

SharePoint på ren svenska