Archive for the ‘ SharePoint 2010 ’ Category

SharePoint 2010 Client Object Model – ECMA Script Get List Fields

I was working on a project that required me to dynamically build a grid based on a SharePoint list using client side JavaScript and naturally, I needed a way of retrieving the fields (columns) of the list. Some research revealed the best way to do this use the SharePoint client object model.

Before we write any code lets make sure that everything we’ll need is ready when our code executes. So, lets wait for the sp.js script to load before we execute our function. We can do that with this snippet of code:

function getFields() {
 // waiting until the core.js is loaded
 ExecuteOrDelayUntilScriptLoaded(retrieveAllFields, 'sp.js');

Now that we’re sure our code will work, lets write it. First, we’ll need to build the client context query in our retrieveAllFields function.

function retrieveAllListsAllFields() {
 // Getting List Title
 var listTitle = 'TestList';
// Building environment
 var clientContext = new SP.ClientContext.get_current();
 var oWebsite = clientContext.get_web();
// Getting all Fields in default view
 this.list = oWebsite.get_lists().getByTitle(listTitle);
 var defaulViewGUID = '{YOUR GUID}';
 var defaultview = list.getView(defaulViewGUID); // Default GUID
 this.listFields = defaultview.get_viewFields();
// Getting Detailed Information on the Fields
 var collList = oWebsite.get_lists();
 this.listInfoArray = clientContext.loadQuery(collList,
// Executing Query
 clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),
 Function.createDelegate(this, this.onQueryFailed));

Now that we have our query ready to go. Let’s create our logic to process our list fields.

function onQuerySucceeded() {
// Setting Site URL
for (var i = 0; i < this.listInfoArray.length; i++) {
 var oList = this.listInfoArray[i];
 var collField = oList.get_fields(); // Getting All Fields From the List
 var listTitle = oList.get_title()
 if (listTitle == this.list.get_title()) {
 var fieldEnumerator = collField.getEnumerator();
 var textInfo = "";
 // Looping through all fields 
   while (fieldEnumerator.moveNext()) {
     var oField = fieldEnumerator.get_current();
     var internalName = oField.get_internalName();

 // Checking to see if the field is in the current view
     var viewFieldEnumerator = listFields.getEnumerator();
     while (viewFieldEnumerator.moveNext()) {
       var currentFieldTitle = viewFieldEnumerator.get_current(); // Getting Title
 var itemTitle = oField.get_title();
       var type = oField.get_fieldTypeKind();
       textInfo = listTitle + " - " + currentFieldTitle + " " + itemTitle + " " + type + " " + internalName;
 // Adding Info

And you’re done! You’ll notice that my code gets the fields in the current view and compares them to the fields returned in the list query.  This way, you’re not getting back every single field in the list schema (including the ugly hidden ones).

As always, if you spot and error or have a suggestion for an improvement let me know and I’ll update my post.



SharePoint Run with Elevated Privileges Best Practices

Running code in SharePoint with elevated privileges can be risky. It’s always important to make sure you’re using it appropriately. I did a quick Google search and found a great list best practices when using it. You can find the list Here.

The post offers a great alternative to running with elevated privileges. Instead, impersonate the SHAREPOINT\system account and use it to instantiate new SPSite and SPWeb objects. Check out the code below. (Courtesy of Soumya Dasari)

 var user = SPContext.Current.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
 var superToken = user.UserToken;
 using (var site = new SPSite(SPContext.Current.Web.Url, superToken))
    // This code runs under the security context of the SHAREPOINT\system
 // for all objects accessed through the "site" reference. Note that it's a
 // different reference than SPContext.Current.Site.
    using(var elevatedWeb = site.OpenWeb())
       // Perform actions as SYSTEM here

jQuery CAML Query – lists.asmx Web Service

So, recently I’ve embarked on a project to create a client side jQuery grid. To do this, I started out by creating a simple table from list data return from a CAML query to the list.asmx web service.

To get me started, I used Jan Tielen’s blog post Here. His example works perfectly accept you’ll need to modify one line of code to make it fully cross-browser compatible.

Change this:

$(xData.responseXML).find(“z\\:row”).each(function() {

to this:

$(xData.responseXML).find(“z\\:row, row”).each(function() {
Continue reading

SPMetal with Anonymous Access

So, I recently ran into a problem with using SPMetal with anonymous access. When Anonymous users access a web part that runs LINQ queries, they’ll get prompted for credentials in SharePoint 2010. The problem is actually related to the way the LINQ code works. It uses the default SPContext.Current to get the site objects. This becomes a problem when you need to run a piece of code with elevated permissions because without rebuilding the SPContext.Current, you’re still using the default permissions. With the default permissions, as an anonymous user, you will not have enough access to make calls using LINQ and as a result, you’ll be prompted to log in.

This blog had an okay solution:

Use the HttpContext object to force all SP objects to be created again. When this happens within the RunWithElevatedPrivileges method, the SPContext is recreated with this higher level of priveleges.

This code has its problems though.

Continue reading

Conditionally Hiding the Ribbon Based on User Permissions

So, I recently needed to hide the SharePoint 2010 ribbon. After trying several different options, I found a very simple, but solid, solution.

To start you’ll need to crack open your master page in SharePoint designer.

Next, find the <div id=”s4-ribbonrow” class=”s4-pr s4-ribbonrowhidetitle”> tag that should be just a few lines below the head tag on the standard SharePoint 2010 V4 master.

Now, you’ll want to add a SPSecurityTrimmedControl to the page like the one below.

Continue reading

SharePoint Ribbon Modifications – Ribbon Buttons Too Small

So I was working on getting some content into the top right hand corner of the ribbon and the strangest thing happened: the ribbon buttons began shrinking themselves as if you’ve re-sized the browser. Check out the image below for an example of what happened.

At first, I thought it was due to a rogue style effecting the ribbon buttons, but with a closer examination of the source of my trouble I realized SharePoint was in fact scaling down the ribbon because of a style I set on a container to the right of the ribbon.

The CSS class that was causing my trouble was the s4-trc-container-menu. I mistakenly applied a float: right and a set width. SharePoint didn’t react well to either of those. The ribbon would shrink all the time with it floated to the right. As for the set width, the Ribbon began to display errors for me at about 400px.

Bottom line: Don’t float anything next to the ribbon, it can cause it to behave erratically.

Populating a Drop Down Based on Content Type

Recently, I’ve had to populate a drop down with lists of a certain content type. So, SharePoint offers a great way to filter lists based on content types with their ContentTypes.BestMatch() and .IsChildOf()  functions.

In order to get the content type you’ll need, you’ll have to know the ID of the content type. Theses are pretty cyptic, but you can find a great MSDN Article containing a description of how content types are composed Here.

Continue reading