Archive for March, 2012

Finding Files Linked To A Specific File (Publishing Pages Linked to a Specific Page Layout)

Monday, March 19th, 2012

Ever had a need to find all of the files in a site collection that link to a specific file? A colleague of mine made an excellent find a few weeks ago and taught me this gem from the SharePoint object model. He had a need to find all publishing page layouts on a site and catalog which were being used and which weren’t. Check out his post here: SharePoint Fix: Powershell script to get SharePoint Page Layouts inventory and its usage across site collection

Passing Parameters to Delegate Controls

Tuesday, March 13th, 2012

I have a requirement for a DelegateControl where I need the child candidate control to render a link that opens in a new window in certain instances of the DelegateControl and open the link in the same window otherwise. The design that made the most sense to me was to use the same code for the child control to compute and render the link in both circumstances and pass a boolean parameter, OpenLinkInNewWindow, to control the target of the link.

The reader might be wondering why I need a control just to render a link. For the purposes of this article, just assume that the control has some built-in intelligence to render a link that cannot otherwise be easily generated!

But how would I pass a parameter to the child control that the DelegateControl uses? Unfortunately, the DelegateControl itself does not have the ability to accept parameters. However, the Control element that defines child controls for a DelegateControl, within the elements.xml file for a feature, does have the ability to accept parameters. Perfect!

To define parameters for a control, one can setup property name/value pairs using the Property element, which is a child of the Control element. It’s pretty straightforward!

Say I have an application page, master page, etc. that needs to put the my fancy link-generating control in two separate places: one opens the link in a new window and one opens the link in the same window. In my page’s ASP.NET code, I have my two separate DelegateControls. I give each DelegateControl its own unique ControlId so that I can differentiate between the two.

With the above DelegateControls, I can use the first one wherever I need the link to open in a new window and the second wherever I need the link to open in the same window.

I would then define the following in the elements.xml file within the feature that adds the children to these DelegateControls.

Notice that I’m using the same control for both of the DelegateControls. I’m only changing the value of the OpenLinkInNewWindow parameter. This will use the same code in both DelegateControls to render the desired link. This allows me to use the LinkNewWindow DelegateControl wherever I want the link opened in a new window and the LinkSameWindow DelegateControl wherever I want the link opened in the same window.

Update: Adding parameters to custom controls
After sharing this article, I had a few colleagues suggest that I add a quick update around how to allow custom controls to accept parameters in the method described above. Parameters for a control can be created by using public properties defined on the control.

As long as the property is public, one can use the method outlined in this article to pass parameters when the control is used with a DelegateControl. If used in a more traditional context on an ASP.NET page, the public property could be used directly in the source code as follows.

There are several attributes that can decorate the public property to enhance its functionality as a property of the control. See the Attributes Applied to Public Properties section of the Metadata Attributes for Custom Server Controls article on MSDN for details.

Stuck Keys?

Thursday, March 8th, 2012

I was in the middle of a SharePoint demo this morning, was typing some input, and all of the sudden my development server was going crazy! It turns out to be a problem I’ve encountered many times before. The “e” key started Windows Explorer. The “r” key started the Run dialog. The list goes on. It’s so frustrating!

The first thing to check is StickyKeys, but being in the midst of a demo and quite certain I was not fiddling with my Shift key, I knew that wasn’t the culprit this time.

I researched a bit and found two potential solutions:

  1. CTRL + ALT + SHIFT + F12
  2. CTRL + SHIFT + Windows Key

The second solution worked for me and cleared up the issue immediately! Hopefully this little tidbit will help someone else; perhaps myself at a later date!