Skip Ribbon Commands
Skip to main content
Sign In
SharePoint Program Manager, Infrastructure
Home
May 14
This Blog has Moved!

​This blog location is shutting down.  While i don't blog much these days--I am moving some of my most popular posts over to a new location:

http://zsharepoint.wordpress.com/

October 29
My First Sandboxed Solution: Gauge1

First a disclaimer:  I am not a SharePoint Developer.  My area of expertise is in administration and architecture design—and only as a hobby do I Master Pages, C#, CSS, and the like.  Please don’t take any code that I write to be a best practice or even “correct”.  The primary focus of this sample is to show the great ways to use Sandboxed solutions to deliver great UI and user experience.

I’m going to try and create a series of posts on how to create a set of easy to use web parts—that anyone with a SharePoint Site collection can use! *Caveat: requires SharePoint 2010 and Sandboxed Solutions to be enabled.  Alright, Lets get started!
 

 

Pre-reqs:
1.      A personal SharePoint 2010 Development Environment (Win7 works!)
2.      Visual Studio 2010
a.      If you get stuck or need to understand how to attach a debugger, check out Paul Stubb’s helpful post.

1. Plan your Web Part

Here’s my plan:  I found this great “Gauge” control by Mike Cherim at Green-Beast.com and thought I would show how to make the same control work on my SharePoint site.  Here’s what Mike’s control looks like:

 


 

So here’s my web part roadmap:
1.      Configurable Settings:
a.      Gauge Title
b.      Gauge Title Color
c.       Gauge “Unit”
d.      Gauge Goal Value
e.      Gauge Actual Value
f.        Gauge Fill Color
2.      Solution Deployment of two background images
3.      Code for rendering the web part

2. Create the Project

 

Open up Visual Studio 2010 and create a new project.  Select “Empty SharePoint Project” (important: do NOT select the Visual Web Part option—this is a full trust code only solution) and follow the wizard—leaving “Deploy as a sandboxed solution” as your selection.  Here I’m calling mine “BlogGaugeProject”.
 




 

3. Code: Create the Web Part & Web Part Panel Options

Our solution starts out empty, so let’s start by adding a web part control.  If you right click on the name of your project in the “Solution Explorer” window you can navigate to “Add > New Item”.

 


Select “Web Part” and give it a name (I’m using “Static Gauge”):


This will create for us a pretty empty script “StaticGauge.cs” to work with—but it also creates some other important files and deployment pieces that we’ll get into later.   First, let’s work on our web part by creating the configurable properties.  These will be controlled on the web part panel which shows up on the right hand side of the page when you click “Edit this Web Part”.

 

We’ll insert each property we want to add (the list from the Design section #1 above) to the “public class StaticGauge : WebPart” section in the following form:
 
 protected string m_CustomVar;
 
 [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("Display Name"),
        WebDescription("Description")]
        public string CustomVar
        {
            get
            {
                return m_CustomVar;
            }
            set
            {
                m_CustomVar = value;
            }
        }
 
Note that I do not add a custom value for “Title”—the web part already has a title field available via: base.Title

 


I’m not going to copy in all the code—the entire solution is available for download Here.

 

4. Code: Render the Controls

The render code is all in the “CreateChildControls” function.  First I need to create a label to load in the CSS from Mike’s gauge.  I put this inside #region tags to keep the page clean:

 

 
 

 

We paste in the CSS to the “CSS goes here” section and clean up the strings.   Note: If you format your strings like I do here, you’ll need to remove Mike’s comments from the CSS to avoid “quote” collisions.

 

Next, we need to override the color inputs for Border and Fill colors—should they be provided in the input panel.  We do this the easy way by just injecting them at the bottom of the style tag with a couple if statements (right before this line: “CSSLabel.Text += "</style>";”):


 

Alright—last piece!  Let’s create the Gauge Label by converting Mike’s PHP to C#. We enter this and some basic calculations into the CreateChildControls block as well to draw the entire component!

5. Configuration: Hide the Web Part Chrome

Since the control has its own border, we don’t want to render the SharePoint chrome as well.  We can configure this by opening up the “StaticGauge.webpart” file in the Solutions explorer:

 


 

We can add this custom property to the “<Properties>” node to remove the chrome:
<property name="ChromeType" type="chrometype">None</property>
 
While where here, we can provide a name and description for our web part.  This is what shows up in the web part adder:
   <properties>
        <property name="Title" type="string">Progress Web Part</property>
        <property name="Description" type="string">Track goal progress through this chart web part</property>
        <property name="ChromeType" type="chrometype">None</property>
    </properties>
 

 

6. Code: Add Pictures to the Solution

Super close!  Mike’s gauge includes two images—so let’s add these to our solution pack to get automatically deployed!  Right click on the Solution Name and again go to “Add > Add New Item”.  This time Select Module and name it GaugeAssets:

 

Now there’s a new assets library that we can use to add objects to the “Assets Library” of the site we connect to.  We can delete the “Sample.txt” file that is there and then by right clicking on the GuageAssets item in the solutions and going to Add > Add Existing Item we can copy the pictures from Mike’s design.  We end up with this:

 

To make these elements deploy into the SiteAssets library (which already exists) we just need to add a URL field to the Module tag:


Now we can ensure our IMG sources always point to this full path by combining the Site Collection URL and this relative path.  For example:

String m_imagepath = SPContext.Current.Site.Url.ToString();
"<img src=\""+(m_imagepath)+"/SiteAssets/GaugeAssets/cdg_tmom.gif\" width=\"60\" height=\"300\" alt=\"\" />"

 

 

7. Code: Run it!

 

Hit F5—and you’ll launch into your SharePoint Site.  If we edit the home page, we’ll see our new web part in the Web Part Adder (in the “Custom” section)!  Drop it on the page and play:
 


  
 

 

The complete sandboxed solution for the web part is available for download in full here.  Source is in a ZIP file here.
Hope this post is useful!  Feel free to give me feedback on the types of posts in the comments section.

 

August 27
Alert Fixup in SharePoint 2010

If you have ever used the SharePoint Admin Toolkit for 2007 you're familiar with the UpdateAlert stsadm command that allows you fix the URLs sent by alerts after a site has changed URLs. This is still an important feature to have in 2010—for example say you re-organize your SharePoint site to map to a new organizational structure.

With the "Power" of PowerShell Microsoft has recreated the UpdateAlert command as a PowerShell script function that you can dot source and use in your own environments. The full instructions and download link is here: http://technet.microsoft.com/en-us/library/cc508847.aspx . Below is a quick demo:

Demo Scenario: I have a site collection http://zsharepoint2 which actually should be located at http://zsharepoint2/sites/alerts.

  1. First I backup the site collection so I can move it (lock it first so changes don't occur during the move!).

  2. Then I restore my backup to the desired URL.

  3. Now I dot source my Invoke-AlertFixup.ps1 file to add the new "Invoke-AlertFixup" command.

  4. I first test the Invoke-AlertFixup using whatif to make sure it will fix my alerts:

    Notice that while 5 alerts were found (count "Whatif" actions) none were fixed.

  5. Then I run it. If there are any alerts using an alternate zone or url which does not match the "OldURL" I provide, it will notify me (you must run this script once per zone):

  6. Here's what it looks like when an incorrect or alternate zone URL is provided:
June 14
SP 101: Adding Additional Default Doc Types to a Document Library

This is a quick SharePoint 101 blog post on document library behavior for SharePoint 2010. By default, when you create a new document library you are asked which document type you want to make "Default" for your list. However, what if you want all four Office document types to show up? Here's what we're going to walk through; getting from this:

To this:

Let's begin! First thing we're going to do is to create the document types for Excel, PowerPoint, and OneNote. (I don't create a word content type since I'm going to use that as my default type, but you can do this if needed using the same method).

  1. Let's first go create our "default template files" for Excel and PowerPoint--you need to have the Office applications installed to do this. Open up each application, and save the "blank" document you get when opening the application to your desktop. For example, Open Excel, click "Save As" and save it to your desktop as "excel_template.xlsx". Repeat for PowerPoint.

     

  2. OneNote is harder since you need to get a "*.onepkg" file. The easiest way to do this is to create a temporary document library that has OneNote as it's default. Here's the steps to extract the default OneNote:
    1. On your SharePoint Site, click New Document Library under the Site Actions menu (normally in the upper left corner)
    2. Provide some temporary data. Make sure you select "Microsoft OneNote 2010" as your default template:

    1. When the list gets created, on the Library ribbon tab click the Library Settings link.
    2. On the settings page, under General Settings click the Advanced Settings link.
    3. The second option on this page is "document template". Highlight the template URL value and Copy it.
    4. In your address bar, remove everything after your "site url" and paste in the document template path from the previous step. Essentially everything from "_Layouts/advsetng.aspx?List=" onward.





    1. Press enter. When you're prompted, press "Save" and save it to your desktop as "onenote_template.onepkg". This is your OneNote template.
    2. To delete this temporary list you can find a Delete this List option on the Library settings page—you can reach this from step g by simply pressing your browsers back button or "Cancel"

     

  3. Now that we have our templates, we need to create our content types. We do the same thing 3 times—once for each document type:
    1. Let's create the excel type. First we navigate to Site Settings from the Site Actions menu
    2. Under the Galleries section, click the Site Content Types link.
    3. On the content types page, click Create.
    4. Fill in the form by: Providing the Name & Description you want users to see in the New Document dropdown (leave description blank to have it say "Create a new <Name>". For the rest of the items, fill it in as you see below (Important:make sure you set the parent content type to "document"):


    1. After created, you're taken to the Content Type management page. Under Settings, click the Advanced settings link.
    2. In the first row, select "Upload a new document template:" radio button, and browse to the Excel "template" you created in step 1 above.

    1. Press "Ok" to save your changes.
    2. Repeat this entire set of steps to create a content type for PowerPoint and OneNote Notebooks.
  4. Last step—setup the Document Library.
    1. First, on your SharePoint Site click New Document Library under the Site Actions menu.
    2. Provide the name and description. Since we created content types for Excel, PowerPoint, and OneNote I'm going to pick "Microsoft Word document" as my default template.

    1. We need to first enable custom content types for this list. In your new document library, click on the Library ribbon tab and click the Library Settings button (almost all the way to the right).
    2. On the Library Settings page, click the Advanced Settings link.
    3. The first option is "Content Types". Set "Allow management of content types" to Yes and then press OK

    1. A new section will have appeared under the menu on the Library Settings page called Content Types. Click the link at the bottom of this section called Add from existing site content types.
    2. Add the three custom content types we created in step 3 by selecting them one at a time and pressing the Add > button. I filter down the list to "Document Content Types" to make them easier to find. When you get something like I have below, press OK

    1. Now on the Library Settings page click the Change new button order and default content type link.
    2. On this page you can order the New Document menu options to your liking. When you're done, click OK
  5. We're done! Note that these content types now exist anywhere in this site so you can just do step 4 above to add these same options to another library.


May 11
Blog has a New 2010 Master Page

I finally made the switch to the 2010 UI; starting with a new master page!  I made this master page pretty quickly so please use the comments of this post to give me any ideas or suggestions for changes as well as to provide any bugs/issues you may come across.

Once I'm "done" with the design and have worked out the issues i'll start blogging about how to skin a 2010 site!

Cheers,
Zach​

1 - 5Next

Quick Launch