Archive for July, 2012

Custom Rich Text Editor Styles in SharePoint 2010

A common request to have in SharePoint 2010, especially externally facing installations, is to have custom Styles available in the menu.

Thankfully, SharePoint 2010 makes this extremely easy. The key is the prefix attached to your styles. SharePoint will interpret any style beginning with ms-rteElement-[custom name] as a markup style and it will interpret anything starting with ms-rteStyle-[custom name] as a Style. However, you are able to change this prefix, but we’ll start out by creating some new styles using the default ‘ms-rte’

First, the differences between the two:

Markup Styles (ms-rteElement):

  • Will wrap the currently selected area in the specified tag (See below)
  • Applied style to newly created tag

Styles (ms-rteStyle):

  • Applies your custom class to the currently selected element
  • Will not add additional markup

Getting Started

Now that we’ve discussed the differences between them, lets talk about how to implement them. To start, make sure you have a stylesheet already linked on your SharePoint master page. Once that’s ready, follow the steps below.

Continue reading


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