Top tips for boosting code efficiency with the SharePoint Client Object Model

Streamlined code approaches for AJAX that couldn’t be easier.

At Shinetech, we’re always on the lookout for new ways to streamline software development to save you time, resources and money. Case in point: How often do you get a request for an AJAX feature in your SharePoint projects? We recently received a client request to implement an AJAX feature in a SharePoint Web Page to retrieve current tasks for a logged-in user. We were able to achieve this with relative ease by using the SharePoint 2010 Client Object Model and jQuery. The results looked like this:  

 

 

It was fast and easy for us, and it can be for you, too, if you follow these simple tips:

 

Tip: Call SharePoint JavaScript Client Object Model asynchronously to get the logged in user.

 

By calling the executeQueryAsync method and passing the callback functions’ signatures in, the functions are executed asynchronously. See example below:

 

function GetLoggedInUser() {

        var context = new SP.ClientContext.get_current();

        var web = context.get_web();

        context.load(web);

        this.currentUser = web.get_currentUser();

        context.load(this.currentUser);

        context.executeQueryAsync(Function.createDelegate(this, this.onGetLoggedUserSucceeded),

        Function.createDelegate(this, this.onGetLoggedUserFailed));

    }

 

Tip: Use CAML to query the data of the current logged in user.

 

Using the ordinary SharePoint Server-Side API plus C# code is time consuming and requires extensive coding to archive the asynchronous feature. However, using the client-end API, we saved time and reduced the complexity of the task because less coding was required. See example below:

 

 camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='AssignedTo'/>" +

                "<Value Type='User'>"+ currentUser.get_title()  +"</Value></Eq></Where></Query><RowLimit>10</RowLimit></View>"); 

 

Finding smarter ways to code is part of our agile development success. Ask us how our expert programmers can help you with your next development project. 

 

Categories: 
up
0 users have voted.

Add new comment