Skip to main content
Kevin Perkins - Microsoft SharePoint, .NET, SQL Server and Webservices Consulting

Tips & Tricks

Go Search
Home
Showcase
Tips & Tricks
Contact
  


Other Blogs
There are no items in this list.
Kevin Perkins.com > Tips & Tricks
Displaying Picture Attachments On A DispForm List Item
I recently came across the need to create a "Product Detail" page, much the way you might have one on a traditional ecommerce site. Rather than roll my own custom code/page, I developed a super easy way to this... I cloned the DispForm, called it Detail.aspx, and added the pictures above it.
 
Here's the basics of it:
  1. Check to see if the item has any attachments
  2. Filter for the file extensions you wish to display
  3. Create an alternate message if no pictures exist in the attachment collection

What It Looks Like

I wanted to give the end-user the ability to attach picture files to be the "previews" for a product. At the same time, I also didn't want to restrict the user from uploading non-picture files (such as PDFs, PPT, etc) in case it's needed.

Using my product detail page below, I have two chair images and a sample text file. In my process, I build out only the images and ignore the text file:

Showing Attachment Inside Display Form

Naturally, I want to error handle this too in case there are no images present on a particular list item:

Show Alternate Text When No Image Attachments Exist

Here's How It Works

  1. Query the list you're working on
  2. Create a string placeholder that will be your HTML output to the page
  3. Determine the existence of attachments on your list item
  4. Handle that existence by writing out the IMG tag and TABLE DATA tags--OR--your alternate message, if no images exist
  5. Set your HTML variable to an <asp:label> tag in the main content placeholder, but above the standard WebPart:

    Product Images Label

  6. Wire up any styles as appropriate

Here's how I did it:

protected void Page_Load(object sender, System.EventArgs e)
{
 if(Request.QueryString["ID"] != "")
 {
  SPList list = SPContext.Current.Web.Lists["Manage Products"];
  SPListItem item = list.GetItemById(Convert.ToInt32(Request.QueryString["ID"]));
  string ProductImages = "";
  ProductImages += "<table width=\"100%\" class=\"ms-formtable\" cellpadding=\"3\" cellspacing=\"\"><tr>";
   if(item.Attachments.Count > 0)
   {
       foreach (string filename in item.Attachments)
       {
        if(filename.Contains(".jpg") || filename.Contains(".gif") || filename.Contains(".png"))
        {
         ProductImages += "<td align=\"center\">";
         ProductImages += "<a target=\"_blank\" href=\"" + item.Attachments.UrlPrefix + filename.ToString() + "\">";
         ProductImages += "<img border=\"0\" height=\"120\" src=\"" + item.Attachments.UrlPrefix + filename.ToString() + "\">";
         ProductImages += "</a></td>";
        }
       }
   }
   else
   {
       ProductImages += "<td width=\"100%\" class=\"ms-formlabel\" align=\"center\"><h3 class=\"ms-standardheader\">[ No product
preview available ]</h3></td>";
   }
     ProductImages += "</tr></table>";
     lblProductImages.Text = ProductImages.ToString();
 }
}

Once I created the proof of concept, I ended up using the built-in WSS styles from the Form Table definition for my alternate text.

Now, every time you add another image to the list item, it just creates another cell within that table equidistantly. The only issue you might encounter is the picture order. You could probably figure another element for ordering, or you can just alphabetize the attachment names to the order you wish them to display since they are returned that way.

PS--Don't forget to change your list's default display page to the new Detail.aspx!

Getting SharePoint Lists To Point To Alternate Forms
I came across a frustrating issue with SharePoint Designer today. I am building a custom Edit form and rather than building a custom XSLT list that fed the right params etc... I desired to have the built-in List redirection handle it.
 
Normally what you'd do is go into your SharePoint site, right click the List that you want to do this to, and get Properties. Upon clicking the Supporting Files tab, you'd be presented the dialog like so....
 
Supporting Files Dialog Box
 
The assumption is, you click "Browse...", select your file that you want it to redirect to and click OK. The trouble is, this doesn't work! If you go back to your list, you're still accessing the default Display, New, and Edit pages.
 
So here's what you do:
 
1) Repeat the same sequence until you get to the Supporting Files interface as above
 
2) Once you're there, you *simultaneously* change your content to type to whatever's there besides "Folder". If this is a Custom List, the other choice is probably "Item". In the case of a Link List, the other choice is "Link", etc. Whatever! just pick it...
 
Choose alternate content type
 
3) Then, click "Browse..." and grab the alternate file you want your list to point to. In my case, the alternate Edit Form...
 
Pick your alternate form
 
4) Apply or click "OK" and close.
 
Once you do that, just double-check by getting Properties on that list again. What you'll see is the new page change, but the Content Type will have flipped back to "Folder". This seems like a bug, so disregard. Your list will go to the new page. The inbound link from your list will still point to "EditForm.aspx" (in the Status Bar of the browser), but that page has the logic to redirect into your new alternate form.
 
Getting StaticSelectedStyle To Work In Your ASP:Menu Control
I had a client desire to have a subnav inserted inside of a custom SharePoint page. Rather than build my own nav, I thought I'd experiment to see if using a regular ASP:Menu control would work. Whatever solution I ultimately came up with had to be easily maintained by non-programmers. So the ASP:Menu seemed as HTMLly as could be.
 
Of course, since SharePoint is .NET-capable by default, I wasn't completely surprised when the control worked...
 
ASP:Menu Control
 
 
I even skinned it up with the same styles as the Top Link bar, so that it would stay in design alignment with the site...
 
ASP:Menu Styles
 
But what was frustrating... was that none of the selected menu nodes were triggering the StaticSelectedStyle attribute. I had the Selected property designated, my NavigateURL property set, etc. But every time the control clicked it wasn't showing a highlighted tab.
 
Then through some experimentation, I discovered that the StaticSelectedStyle attribute will not fire with the NavigateURL property! But it will fire if you put your URLs in the Value property...
 
ASP:Menu Selected
 
This was my result....
 
StaticSelectedStyle ASP:Menu
 
My guess is the NavigateURL property must do a GET, and the Value property must do a POST. Therefore, the POST probably carries over the viewstate to tell the control what to highlight.
Popping SharePoint QuickLaunch Links Into New Window (... And Any Other Links On Your Page)
A friend of mine and I were talking about a "simple" way to make links in the QuickLaunch (QL) menu open in a new window. In our case, we want the slideshow of a Picture Library to be listed on QL, and have it pop open to a new window that can then be closed after viewing. Currently, if you don't pop the link open into a new window, the link goes straight to the slideshow page. But, the parent links in the slideshow page open up--yet again--additional new windows when you try to go back to the main site or subsites in the Asp:SiteMapPath control. It's definitely not the best user experience
 
At first glance, the idea of "targeting" a new window from QL seems easy. But, after you explore both the browser-based interface to QL--as well as via SharePoint Designer--the <SharePoint:AspMenu> control isn't easily configured on an item-by-item basis. Of course one could attempt to build a custom webpart, and go that route. But, what is meant to be a simple "thing" ends up consuming quite a bit of time determining the best way to implement.
 
JavaScript in QuickLaunch
 
As you may know, the browser-based QL link manager allows you to build standards-based <a href=""> values to show links on the left side a standard WSS or MOSS layout. Maybe an (un)known secret to some is that those hef values can hold a standards-based javascript:; call as the value of the href.
 
For example, in the QL link manager, if you add a javascript command as if you were building standard HTML...
 
Hello World!
(Be sure to use single quotes when designating string values; full quotes will cause an error)
 
 
... you will see a link in your QL area...
 
 
Hello World Link
 
 
... that when clicked, produces your standard message box:
 
 
Hello World Dialog
 
 
 
Make A Reusable Function
 
Now, the logical progression in this is to leverage the fullness of the JavaScript environment in SharePoint. By creating a JavaScript function in your masterpage, not only can you control the target of your QL links, but you can reuse this function for any links on a page inheriting your masterpage.
 
(Note: be sure you make a backup of your masterpage before attempting this)
  • Step #1 - Open your masterpage in the SharePoint Designer site where you want to have this javascript command exposed
     
  • Step #2 - In the Code view, insert a client-side JavaScript tag at the end of your <head> tag that has a funtion that calls a window.open method, and save your masterpage...
     
    openWin function
     
  • Step #3 - Open the QL link manager in your browser and type in the javascript call for your link...
     
    openWin Link
     
    ... where [your page] is your valid SharePoint site URL. Of course, you can expand your javascript:openWin(...); call to also include feature and replace values.

This is all typical JavaScript functionality; just make sure it all conforms to standards, and you can program any client fucntionality into your QL links... or any other links on your page.

Bonus!

So, at the beginning of the article I mentioned the whole Picture Library issue how the View Slide Show menu creates a lot of unnecessary targeting for subsequent pages. Well, if you're building a QL to this page, it'd be a better user experience if you negate the parent links on the slideshow.aspx page.

The simplest way to do that is to just kill the SiteMapPath control on your particular slideshow.aspx page. Just go into SharePoint Designer, and open your: Picture Library --> Forms --> slideshow.aspx. Then, locate the <asp:SiteMapPath> tag, and remove it...

Picture Library SiteMapPath

... you'll be prompted to overwrite the definition, which you should do. Alternately, you can set the ParentLevelsDisplayed equal to 0 to only show the current node without any parent links.

 ‭(Hidden)‬ Admin Links