Locating all of the Web Parts on the Current Page (C#)

The following snippet allows for locating all of the web parts on a given web part page (the sample is targeted at web parts of the type Microsoft.SharePoint.WebPartPages.ListViewWebPart)

string webUrl = SPContext.Current.Web.Url;

using (SPSite site = new SPSite(webUrl))
     using (SPWeb web = site.OpenWeb())
          SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(this.Parent.Page.Request.Url.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

          SPLimitedWebPartCollection webPartColl = manager.WebParts;

          for (int i = 0; i < webPartColl.Count; i++)
               if (webPartColl[i].GetType() == typeof(Microsoft.SharePoint.WebPartPages.ListViewWebPart))
                    // do stuff

Tip: JavaScript to enable opening links in a new window in SharePoint

There are times when developing in SharePoint where you will be required to modify existing links on a page to make those links open in a new window (Document Library, Links List, etc.).  There are times when the original anchor tag carries an “onclick” event of “GoToLink(this); return false;”.  The original “GoToLink” function will not allow for opening even links with a modified target in a new window.  The following is a piece of JavaScript that will allow for overriding the original “GoToLink” function.

Note:  For those links where I truly wish to override the “GoToLink” behavior, I attach a custom attribute to the anchor to be sure it is truly a link I have manipulated.

//Create a custom GoToLink function
var _origGoToLink = GoToLink;
GoToLink = function(elm) {
  if (elm.href == null)
  var ch = elm.href.indexOf(\"?\") >= 0 ? \"&\" : \"?\";
  var srcUrl = GetSource();
  if (srcUrl != null && srcUrl != \"\")
    srcUrl = ch + \"Source=\" + srcUrl;
  var targetUrl = elm.href + srcUrl;
  if (elm.getAttribute('cnWebPartsHref') != null) {
    //Use window.open rather than window.location
  else {
    window.location = STSPageUrlValidation(targetUrl);

So you want to learn about SharePoint?

If you’re looking for a primer in programming with SharePoint, the following are a few good resources provided by Microsoft:

  • MSDN Ramp Up
    • From the site: Ramp Up is a free, online, community-based learning program, with a number of different tracks that will help you build your portfolio of professional development skills. Ramp Up has a solid foundation of premium technical content from subject-matter gurus, and provides easy-to-access content in a variety of forms that guide you in learning the important skills
  • SharePoint Developer
    • A primer series on developing with SharePoint from Microsoft.

When Timer jobs go bad…

Occasionally, for no good reason, administrative timer jobs can go bad, leaving them in a state of limbo.  For instance, have you ever deployed a solution package and the status remains at “Deploying”?  When behaviors like this begin to appear, the timer jobs in question can still be forced through by utilizing STSADM.

The execadmsvcjobs switch will force any pending jobs to execute immediately (such as a deployment gone bad). Execute the command on each server in your SharePoint farm, and the timer job should clear itself.

stsadm -o execadmsvcjobs

Best Practices for Enterprise User Scalability in SharePoint

Joel Oleson is at it again, this time with an excellent primer on user scalability in a SharePoint environment.  The article makes for an excellent primer on how to manage users in SharePoint environments in a better manner.

Getting Started With Windows PowerShell and SharePoint

Today was my first experience with PowerShell, and I have to say I’m impressed.  PowerShell truly does appear to be a command prompt for the .NET generation.  While it can be agravating at times, I can see PowerShell quickly becoming a “can’t do without” tool in my arsenal.


Integrating ASP.NET AJAX Extensions (1.0) With SharePoint

Mike Ammerlaan has an old(er) post up on his blog relating to integrating the ASP.NET AJAX Extensions (1.0) into SharePoint.  While his post does follow the standard integration instructions for the most part, he does offer some insight into including a ScriptManager in any custom web parts as well as working with UpdatePanels (which can be troublesome prior to MOSS 2007 SP1).

I might play around with implementing a custom receiver to implement the web.config changes as more and more clients begin to request this functionality in their MOSS implementations.

Inheriting the Current Master Page for a Custom Application Page

To inherit the master page from the current site being browsed in a custom application page (i.e. a page that resides in the “_layouts” directory), you can use the following PreInit method:

private void Page_PreInit(object sender, EventArgs e)
            System.Web.UI.Page page = sender as System.Web.UI.Page;
            if (page == null) return;
            if (page.MasterPageFile == null) return;

            HttpContext context = HttpContext.Current;
            string currUrl = context.Request.Url.ToString();
            string basepath = currUrl.Substring(0, currUrl.IndexOf(context.Request.Url.Host) + context.Request.Url.Host.Length);
            //Use RawUrl, otherwise it will always use the root web.
            currUrl = context.Request.RawUrl;
            if (currUrl.ToLower().Contains("/_layouts/") &&
                currUrl = currUrl.Substring(0, currUrl.ToLower().IndexOf("/_layouts/"));
                using (SPSite site = new SPSite(basepath + currUrl))
                    using (SPWeb web = site.OpenWeb(currUrl))
                        page.MasterPageFile = web.MasterUrl;

Just add the code above to your code behind, and your custom page will now inherit the look and feel of the site it is currently in.

Session Timeouts, Windows Authentication, and SharePoint

Over at (slightly) Behind the Curve, there is a posting on modifying your master page to allow for Session based timeouts for an integrated Windows security site. I found it to be quite helpful for a solution that is being implemented at a client for an extranet.