How to get the logged on users name from a SharePoint page using javascript

Often I have wanted to get the user’s name, or other user properties, when a SharePoint user hits my site. Instead of writing code against the SharePoint object model within a custom web part, there are two additional methods to consider if you are trying to accomplish the same thing. One is to look at the source of the page to find the element that contains this information, or to query the SharePoint web services.

(more…)

How to use the Content Editor Web Part to build a dynamic link based on your current URL

Believe it or not, this is a very useful piece of code. I use it often in my content editor web parts to get the URL of the site I’m currently on.

To be more specific, I use this code to get the URL of the site, then modify it to dynamically create a URL that points to the LISTS.ASMX web service. Why is this useful? Consider the following:

  1. I have a custom list at the root site collection that is readable by everyone.
  2. The list contains a link to a custom web part page that is located on each site (subsite) in my site collection. The web part page is just a blank page that has a few CEWP’s (content editor web parts) in different zones.
  3. One of the CEWP’s only function is to get the URL of the site, then does a javascript ‘split’ to get each element of the URL. This is necessary to build a custom URL that points to the ‘LISTS.ASMX’ web service for the site I’m on.
  4. Once I have the new URL (that has been modified to point to the ‘lists.asmx’ web service), I can then query my sites schema (including list schema) for the site.

I find this to be VERY useful in that I can get a listing of all lists and libraries for a site and view the XML schema behind them, all bundled up in a single report. Instead of going thru each site and each site’s lists one at a time, I can view the XML schema from a single ‘web part page’.

So, to get started, I’m going to show you how to get the URL from a site. Let’s begin by assuming you are on a SharePoint site at this URL: (more…)

How to use a Content Editor Web Part to get the value of a query string

I often use query strings in SharePoint web parts, links, lookup columns, etc., and have found this bit of code to be very useful when having to display content on my page based on the value of a query string parameter. Here’s an example of how to get the value of a query string parameter by using JavaScript:

(more…)

Display an error if an unsupported browser views your SharePoint site

When I create web parts for my SharePoint sites, I try to make it a good practice to detect what browser is viewing my site and then display a message if it is not supported.

For instance, the company I work for only supports Internet Explorer. I am able to use other browsers in my organization but the content on any web page is only guaranteed to be displayed correctly within IE. All other browsers may or may not display the information correctly, so IE is the only option I have to work with.

So, I often use Content Editor Web Parts to display content on my site, especially if I am using javascript to query my SharePoint sites for data (using the SharePoint web services). However, if a user goes to one of my sites and gets errors or has improperly formatted data, I have to instruct them on the company’s policy to use IE only to view the site. In most cases, this doesn’t go over well.

To use this code example, add the code to a Content Editor Web Part on your site (I usually put it at the top): (more…)