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.