Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Can multiple script editor web parts share a client context object?

$
0
0

We are developing a homepage with a few script web parts. Each web part has a reference to its own JavaScript. The web parts retrieve information from different SPLists. My web parts are somehow interferring with each other. If i remove the first one, the second works and vice versa. The code for each web part is similar to this:

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getSomeInfo);
});

function getSomeInfo() {
	var context = new SP.ClientContext.get_current();
	var oList = context.get_web().get_lists().getByTitle('MyList');
	var camlQuery = new SP.CamlQuery();

	camlQuery.set_viewXml('<View><Query><Where></Where></Query><ViewFields><FieldRef Name="Title" /><FieldRef Name="Link" /><FieldRef Name="Category" /></ViewFields></View>');
	this.collListItem = oList.getItems(camlQuery);
	context.load(this.collListItem, 'Include(Title, Link, Category)');

	context.executeQueryAsync(Function.createDelegate(this, this.someInfoSucceeded), Function.createDelegate(this, this.someInfoFailed));
}

function someInfoSucceded(sender, args) {
	var listItemEnumerator = collListItem.getEnumerator();
	while (listItemEnumerator.moveNext()) {
		var oListItem = listItemEnumerator.get_current();

		var title = oListItem.get_item('Title'); // This always works
		var linkDescription = oListItem.get_item('Link').get_description(); // Returns error
		var linkUrl = oListItem.get_item('Link').get_url(); // Returns error
		var category = oListItem.get_item('Category'); // Returns error

		// Code here to handle list item information...
	}
}

During the iteration I always get the title value, probably because it's an internal field. Every try to get the value of a custom field gives the following error:

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Remember: Every web part works fine when it exists alone on a page. But when I put another one on that page, one will work and one not. This leads me to believe that it has something to do with the client context. Maybe I shouldn't try to load it for every web part? If that is the case, how can I make them share a context? Use the master page to load sp.js and sp.clientcontext?

Please help me out, because I'm getting more confused by the minute.



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>