Thursday, December 17, 2015

People Picker MVC Sample


OfficeDev PnP has a great sample for using the people picker in a provider hosted add-in. The only problem is that the sample uses web forms. In this post I'll demonstrate using the people picker in an MVC application.

Create a new App for SharePoint

Make it Provider hosted
Choose MVC Web Application
Choose Windows Azure Access Control Service

Add a dummy module to the SharePoint project

Right click the SharePoint project and add a dummy module. This will allow you to easily get a SharePoint context.

Edit _Layout.cshtml 

Add MicrosoftAjax.js and a Styles section. Remove the menu and footer.


Copy files from the PnP Project Scripts folder into the MVC Scripts folder

app.js
peoplepickercontrol.js
peoplepickercontrol_resources.en.js

Copy files from the PnP Project Styles folder into the MVC Content folder

peoplepickercontrol.css

Edit the app.js file

Add functions for the chrome control, renderSPChrome and chromeLoaded.


Add a people picker to index.cshtml




Lets walk through this.

Line 1 add the CSS for the control.

Line 5 this is for loading the SharePoint chrome control. If this is not what you want you will need to include the SharePoint core.css file into your project or at least those bits needed for the people picker.

Lines 9 - 19 this is the html needed for the people picker. The ids are important. They are used by the JavaScript to build the control.

Line 29 add the people picker JavaScript files to the page


That's it. If you've followed along the people picker should be up and running in your MVC application. Full source code can be found here https://github.com/spkrby/MVCPeoplePicker.git











Wednesday, April 1, 2015

Use Powershell with SharePoint Online

Install Powershell on your machine

http://www.microsoft.com/en-us/download/details.aspx?id=40855

Install SharePoint Online Management Shell

http://www.microsoft.com/en-us/download/details.aspx?id=35588

Connect to SharePoint


Connect-SPOService -Url https://???????-admin.sharepoint.com -C
redential ?????@?????.onmicrosoft.com

I can't connect, I get the following error: "Current site is not a tenant administration site."
Fix: You must connect to the administration site. You're administration site will be something like this https://achme-admin.sharepoint.com


Sunday, March 29, 2015

Deploy JavaScript to SharePoint Online with a Console App


One of the new challenges facing SharePoint professionals is adding JavaScript files to SharePoint online. The most direct and simple approach that I have found is to use the UserCustomAction class to add a ScriptLink. To make the deployment a snap, use the SharePointOnlineCredentials class within a console application. The SharePointOnlineCredentials class allows you to access the site using your credentials. You will need full control permission to run this solution.

WARNING, improper use use of a UserCusomAction to add a ScriptLink WILL break your site/site collection in a very disturbing fashion. SharePoint will gladly serve you an absolutely blank page with no error. Therefore, it is recommended to test this solution prior to a production deployment.

Create a new Console App


Add references to SharePoint

Microsoft.SharePoint.Client
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll

Use SharePointOnlineCredentials

First, collect the url, logon and user password from the console. Create a new client context and set the credentials of the context to a new SharePointOnlineCredentials class.

static void Main(string[] args)
{

     Console.WriteLine("Enter SharePoint online url:");
     string url = Console.ReadLine();

     Console.WriteLine("Enter SharePoint online Login : ");
     string logon = Console.ReadLine();

     Console.WriteLine("Enter your password.");
     SecureString password = GetPasswordFromConsoleInput();

     using (var context = new ClientContext(url))
     {
           context.Credentials = new SharePointOnlineCredentials(logon, password);
     }

     Console.WriteLine("Press Enter to End");
     Console.ReadLine();
}


private static SecureString GetPasswordFromConsoleInput()
{
     ConsoleKeyInfo info;

     //Get the user's password as a SecureString
     SecureString securePassword = new SecureString();
     do
     {
         info = Console.ReadKey(true);
         if (info.Key != ConsoleKey.Enter)
         {
              securePassword.AppendChar(info.KeyChar);
         }
     }
     while (info.Key != ConsoleKey.Enter);
         return securePassword;
}

Use the Utility Functions

The downloadable code has a few functions built to list, add and remove ScriptLinks. I would start by just listing them out. The functions are executed directly after SharePointOnlineCredentials. 

/// <summary>
/// adds a scriptlink to the site 
/// </summary>
/// <param name="ctx"></param>
/// <param name="file"></param>
/// <param name="seq"></param>
private static void AddScriptLink(ClientContext ctx, string file, int seq)
{
// Register Custom Action
     var customAction = ctx.Site.UserCustomActions.Add();
     customAction.Location = "ScriptLink";
     customAction.ScriptSrc = file;
     customAction.Sequence = seq;
     customAction.Update();
     ctx.ExecuteQuery();

     Console.WriteLine("ScriptLink Added : {0}", file);
}
        
/// <summary>
/// remove all customactions from the site
/// </summary>
/// <param name="ctx"></param>
private static void ClearAllScriptLinks(ClientContext ctx)
{
     var customActions = ctx.Site.UserCustomActions;
     ctx.Load(customActions);
     ctx.ExecuteQuery();
     customActions.Clear();
     ctx.ExecuteQuery();

     Console.WriteLine("All SriptLinks removed");
}

/// <summary>
/// list the scriptlinks on the site
/// </summary>
/// <param name="ctx"></param>
private static void ListScriptLinks(ClientContext ctx)
{
     var customActions = ctx.Site.UserCustomActions;
     ctx.Load(customActions);
     ctx.ExecuteQuery();
            
     foreach(UserCustomAction ua in customActions)
     {
         if (string.Compare(ua.Location, "ScriptLink", true) == 0)
         {
              Console.WriteLine("Script Source : {0}, Sequence : {1}", ua.ScriptSrc, ua.Sequence);
         }
     }

     if(customActions.Count == 0)
     {
         Console.WriteLine("No ScriptLinks found for {0}", ctx.Url);
     }
}
 

/// <summary>
/// remove a scriptlink matching script source
/// </summary>
/// <param name="ctx"></param>
private static void RemoveScriptLink(ClientContext ctx, string scriptsource)
{
      var customActions = ctx.Site.UserCustomActions;
      ctx.Load(customActions);
      ctx.ExecuteQuery();

      foreach (UserCustomAction ua in customActions)
      {
          if (string.Compare(ua.ScriptSrc, scriptsource, true) == 0)
          {
               Console.WriteLine("Removing Script Src : {0}, Sequence : {1}", ua.ScriptSrc, ua.Sequence);
               ua.DeleteObject();
          }
      }

      if(ctx.HasPendingRequest)
      {
         ctx.ExecuteQuery();
      }
}

Download Source Code

https://github.com/spkrby/SriptLinkUtil

References


http://blogs.msdn.com/b/kaevans/archive/2014/02/23/call-o365-using-csom-with-a-console-application.aspx
http://blog.mastykarz.nl/deploying-custom-actions-app-model/
http://www.ashokraja.me/post/Refer-Scripts-and-CSS-Style-Sheet-in-SharePoint-2013-Visual-Web-Part-and-Master-Page.aspx




Sunday, February 27, 2011

Create Site Collections from Custom Site Templates

In SharePoint 2010 creating templates from sites is as simple as a few clicks. Using that template to create new site collections is a bit more involved. The standard approach goes something like this; download the newly created template, in central administration create a new site collection without specifying a template, navigate to the new site collection, when prompted for the site template upload your custom template. The problem with this approach it's too repetitive if you're going to create a lot of sites and site collections.

What I wanted to do was to select the template to create new sites and site collection just like the builtin ones. To do this you need to understand what's going on when you create a site template from the SharePoint interface. It's a solution file so we can rename it to a .cab file and take a look inside. Along with the manifest.xml file, there are folders for; ListInstances, Modules, PropertyBags, and WebTemplate. The webtemplate folder is the giveaway, what has been created for us is a definition for a webtemplate. The webtemplate folder contains a feature with a scope of "Site" that needs to be changed to a scope of "Farm".
That's it, this webtemplate can now be used to create site collections from central administration, after installation of coarse.



The steps; create the template, download the template, rename the template to .cab, extract the .cab file to a folder, modify the feature xml, convert folder back to .cab, rename to .wsp, add and deploy the solution. To convert the folder back to a cab file I used TUGZip.

Thursday, February 24, 2011

Disable ASP:Button onclick

A common requirement is to disable a button after it's been clicked. It can be accomplished by modifying two properties of the asp:button control. First, set the UseSubmitButton to "false". Second, modify the OnClientClick property to include "this.disabled=true;". These two settings will work in most cases. A problem will crop up if client side data validation is being used. The button will remain disabled and the user will not be able to click the button again. If you're using .Net validation controls you will need to check Page_ClientValidate() and then disable the button. 


<script type='text/javascript'>
   function disableBtn(control) {
     if (typeof Page_ClientValidate == 'function') {
        if (Page_ClientValidate()) { control.disabled = true; }
     }
     else
     { control.disabled = true; }
   }
</script>

<asp:Button runat="server" ID="btnSubmit" OnClientClick="disableBtn(this);" UseSubmitBehavior="false" OnClick="btnSubmit_Click" Text="Submit" />



Tuesday, December 28, 2010

SharePoint People Picker Limit Selection

By default, the people picker will let a user select anyone visible to the picker. This is not always an ideal situation. Luckily, there are a couple of ways to easily limit what the user can select from.

First, if you need to limit selection to site collection users, you can use an stsadm command

stsadm -o setproperty –url http://sitecollectionURL
       –pn peoplepicker-onlysearchwithinsitecollection –pv yes


There are a couple of other options to limit selection that involve setup in active directory. They can be found here; Keep it Simple!
Peoplepicker: Stsadm properties


Second, customize the people editor control with the SharePointGroup property. I've found this solution very helpful in customizing input screens as it gives really fine grained control over an individual field. The field below will only allow selection from "SomeGroup".

<SharePoint:PeopleEditor ID="plpEdit" runat="server" SharePointGroup="SomeGroup" />

Sunday, December 26, 2010

SharePoint People Picker Multiple Domains

The people picker is an important part of a SharePoint farm. Making sure that the correct users are available for selection is key. Issues almost always show up in farms where multiple domains need to be available for selection. The fix is a couple of stsadm commands, these commands work in SharePoint 2007 and 2010. 

By default, the application pool identity is used to search active directory. If the account does not have the correct permissions, you will need to encrypt the password for the account that will be used to search that domain. This account needs to be noted for password changes!

Set the encryption key (run on each WFE)
stsadm -o setapppassword -password  *********
Set the domains that should be searched  (run on one WFE per web application)
stsadm -o setproperty -pn peoplepicker-searchadforests 
       -pv domain:domain1;domain:domain2,domain2\account,password 
       -url http://webapp
A more detailed discussion can be found here:
http://blogs.msdn.com/b/joelo/archive/2007/03/08/cross-forest-multi-forest-configuration-additional-info.aspx

Visual Studio 2010 Code Snippets

Code snippets are a great way to speed up development time. In this post I'll go through a simple way to create your own code snippets. I'll be creating a snippet to log to the 14\LOGS files.

Open a Visual Studio project and add a new xml file. Make sure to save it with a .snippet extension.

Right click in the new xml file,  select Insert Snippet and double click on snippet. The xml below will be inserted for you.



Fill in the values you would like for these tag. The most important is the shortcut - its what will start intellisence for you.
    <Title>SharePoint Logging</Title>
    <Author>Me</Author>
    <Shortcut>SPLog</Shortcut>
    <Description></Description>

Choose what type of snippet you want either SurroundsWith or Expansion, I'm using expansion here.
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>

Now enter what you would like to replace in instances of the snippet. Here the component name to be logged changes. If there are more replacements needed, add more Literal tags.
    <Declarations>
      <Literal>
        <ID>Component</ID>
        <Default>CustomComponent</Default>
      </Literal>
    </Declarations>

Change the code language to CSharp.
    <Code Language="CSharp">

Enter the code to be inserted in CDATA[ YOUR CODE GOES HERE  ]
  <![CDATA[
  SPDiagnosticsService.Local.WriteTrace(0,
                    new SPDiagnosticsCategory("$Component$", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
  ]]>


Save the snippet to the default location "C:\Users\Administrator\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets" and your ready to try your new snippet.

Completed snippet file
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>SharePoint Logging</Title>
    <Author>Me</Author>
    <Shortcut>SPLog</Shortcut>
    <Description></Description>
    <SnippetTypes>

      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Component</ID>
        <Default>CustomComponent</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[
  SPDiagnosticsService.Local.WriteTrace(0,
      new SPDiagnosticsCategory("$Component$", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
  ]]>
    </Code>
  </Snippet>
</CodeSnippet>

Logging in Sharepoint 2010

Logging in SharePoint 2010 is a pretty straight forward affair. The code block below will write to 14\LOGS 

using Microsoft.SharePoint.Administration;

                SPDiagnosticsService.Local.WriteTrace(0,
                    new SPDiagnosticsCategory("CustomComponent", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);

If you want to create your own custom logging component, the following two blogs will assist you.

http://blog.mastykarz.nl/logging-uls-sharepoint-2010/
http://www.sharepointproconnections.com/article/sharepoint-development/SharePoint-Logging-What-s-New-in-SharePoint-2010.aspx

Sunday, October 3, 2010

ASP.NET Align Labels with Text

The simple task of aligning labels and text can become not so easy when you move away from using tables. In this example I'm wrapping the lables and textboxes in div and using a bit of CSS to produce the result below. The important bits are highlighted.


div{
      clear:left
      margin: 5px 0 0; padding: 1px 3px;
      width: 400px;
}
.input{
      display:block; float: left 
      margin: 0 0 5px; padding: 3px 5px;
      text-align:right;width: 130px;
}




<div>
      <asp:Label AssociatedControlID="txtOne" CssClass="input" ID="label1" runat="server"  
      Text="Text One:"></asp:Label>
      <asp:TextBox ID="txtOne" runat="server"></asp:TextBox>
</div>
<div>
     <asp:Label AssociatedControlID="txtTwo"    CssClass="input" ID="label2" runat="server"
     Text="Text Two:"></asp:Label>
     <asp:TextBox ID="txtTwo" runat="server"></asp:TextBox>
</div>
<div>
     <asp:Label AssociatedControlID="ddlOne" CssClass="input" ID="label3" runat="server" 
      Text="DropDown:"></asp:Label>
     <asp:DropDownList ID="ddlOne" runat="server">
           <asp:ListItem>Choice1</asp:ListItem>
           <asp:ListItem>Choice2</asp:ListItem>
     </asp:DropDownList>
</div>
<div>
    <asp:Label AssociatedControlID="txtThree" CssClass="input" ID="label4" runat="server"  
    Text="Text Three:"></asp:Label>
    <asp:TextBox ID="txtThree" runat="server" Rows="5" TextMode="MultiLine"></asp:TextBox>
</div>
<div>
    <asp:Label AssociatedControlID="txtFour" CssClass="input" ID="label5" runat="server"
    Text="Text Four:"></asp:Label>
    <asp:TextBox ID="txtFour" runat="server" Rows="5" TextMode="MultiLine"></asp:TextBox>
</div>

Saturday, July 31, 2010

Enable IntelliSense for SharePoint Client Object Model in Visual Studio 2010

Microsoft released some guidance on enabling intellisense for the SharePoint client object model and was disappointed, it only gave partial intellisense. After a bit of investigating, I found that there are a number of debug.js files in the layouts folder. Adding the SP.Core.debug.js file gave me the result I was looking for. 

<script type="text/javascript" src="/_layouts/MicrosoftAjax.js" ></script>
<script type="text/javascript" src="/_layouts/SP.debug.js" />
<script type="text/javascript" src="/_layouts/SP.Core.debug.js" />


Sunday, July 25, 2010

Table of Contents Web Part

The table of contents web part isn't showing all the sites. Go to site settings, navigation and increase the number of sites to display.

Thursday, July 22, 2010

Windows 2008R2 boot VHD

I was in need of a faster SharePoint 2010 development environment and saw some posts on booting from a vhd and decided to give it a try. I'm running Windows 7 ultimate on my laptop and will install Windows 2008 R2 on the vhd. The first thing I tried was converting my virtualbox hard drive. Big waste of time, the conversion from vdi to vhd just didn't work. So, it was time to create a new vhd and install everything all over again. In this post I'll go thorough the steps to create a new vhd and install Windows 2008R2 on to it.

Change the bios settings on your machine so the dvd will boot before the hard drive.

Boot your machine from the install dvd. Oh, you don't have an install dvd, you have an iso file don't you? So you need to create an install dvd from the iso file. It's not so bad. Use a utility like ImgBurn to unpack the image file to disk, you'll see all the setup files when your done. 

Boot your machine from the install dvd. Select your language and click next.


Go to the command prompt  - enter shift+F10
x:\sources > diskpart
diskpart > create vdisk file=c:\win2008r2.vhd maximum=40000 type=expandable
diskpart > select vdisk file=c:\win2008r2.vhd
diskpart > attach vdisk
diskpart > exit
x:\sources > exit

Install the operating system - you will be able to select your new vhd later.

Select the disk you just created, there will be more than one to choose from. Choose carefully. Ignore the warning that you can't use the disk.

Continue with the installation. 

When the installation is finished, remove the dvd from the drive and restart your machine. You will be able to choose between Windows 7 and Windows 2008R2.

Saturday, June 26, 2010

Migrating to SharePoint 2010

Recently I've been migrating farms from SharePoint 2007 to 2010 using the attach database method. In this post I'm going to outline the steps I've used to perform the upgrade. I'm assuming SharePoint 2010  has been set up and that the visual compatibility mode will be used. 

Clean Up Your Current Environment 
Remove unused site collections, features, etc. Run "stsadm -o preupgradecheck" to identify potential problems.

Find the Customizations
Your disaster recovery plan will really come in handy here. If you don't have a disaster recovery plan, this will be a good start for one. Go to central administration and note your settings. They will need to be used to configure your new farm. Find the custom code and files that have been deployed to your farm. If solutions have been used to deploy customizations, they can be used to deploy to the new farm. Make copies of the web.config files, many of the custom settings have been made by hand. Think safe controls, http modules, application settings, connection strings. Run a comparison between your 12 hive and an oob 12 hive to find any differences. Check IIS for any folders or applications that have been added.

Copy your Content Databases
The databases can be detached and copied or backups can be taken. Copy the .mdf files to the new SQL Server.

Install Customizations to the new 2010 Farm
Install your solutions and features, move files into the 14 hive, edit the web.config files... Create new Web Applications for the ones you’re moving. Delete the configuration databases that are created.  

Attach the Content Databases to the new SQL Server 


Test Content Database against the Web Applications
Run Powershell command:     Test-SPContentDatabase -Name "contentdbname" -WebApplication "http://webapplicationurl"  This will give you the guids of missing features and let you know if there are any errors that would stop the upgrade.  

Attach the Content Database to the farm
Run Powershell command:     Mount-SPContentDatabase -Name "contentdbname" -WebApplication "http://webapplicationurl"

Test the upgraded farm

Wednesday, May 12, 2010

Copy DLL from GAC

From time to time you will need to copy a dll from the GAC or assembly. The easiest way I've found to copy the file is to open a command prompt and enter the following command.

           SUBST M: C:\Windows\Assembly

This will create a new drive M: that you can use to copy the file. 

Monday, May 10, 2010

Content Query Web Part - Common Enhancements

The content query web part affectionately known as CQWP is one of the go to oob SharePoint components. It's really great at rollups on content types within a site collection. The CQWP is built with xslt so this post will focus on modifications to "ItemStyle.xsl" or your own custom item style file. Be careful of white spaces and line returns when formatting the XSLT. A couple of tools that will help a great deal with the CAML and fields are U2U CAML query Builder, SharePoint Manager and SharePoint designer.

Use additional fields

First, export the webpart and open it in notepad. Edit the CommonViewFields property. The fields are formatted like this "internal name","type"; After adding the fields, save the file and import it back into the site. Test and make sure the web part still works. If not check names, types, white space and new lines.
   <property name="CommonViewFields" type="string">Sorted1,Text;Status,Text;CDate,Date;
   </property>

Formatting Dates

Add the highlighted line to the xsl:stylesheet tag.
<xsl:stylesheet
  version="1.0"
  exclude-result-prefixes="x d xsl msxsl cmswrt"
  xmlns:x="http://www.w3.org/2001/XMLSchema"
  xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
  xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">

You can then format the date like this.
<xsl:value-of select="ddwrt:FormatDate(string(@CDate) ,1033 ,1)"/>

Headers and Footers

There are a number of posts out there on this subject but this is what has worked for me and has been the least painful to develop. First create variables for the header and footer and then use them at the top and bottom of the template.
   <xsl:variable name="HEADER">
     <xsl:if test="count(preceding-sibling::*)=0">
          <![CDATA[<table border="0" cellspacing="0" width="100%">
                <tr>
                <th>Header1</th>
                <th>Header2</th>
                <th>Header3</th>                </tr>]]>
      </xsl:if>
   </xsl:variable>

   <xsl:variable name="FOOTER">
     <xsl:if test="count(following-sibling::*)=0">
         <![CDATA[ </table> ]]>
      </xsl:if>
   </xsl:variable>
       You can then use them like this.
<xsl:value-of select="$HEADER" disable-output-escaping="yes"/>
<xsl:value-of select="$FOOTER" disable-output-escaping="yes"/> 

Only Show the Most Recent Entry

This is common requirement when rolling up status reports. This technique requires that the list items be in the correct order. To ensure that the list items are in the correct order the web part itself needs to be modified. To do this export the webpart and open it in notepad. Edit the QueryOverride property with the correct CAML statements. Save the changes and import the web part. (Any valid CAML can be placed in the QueryOverride tag.)
   <property name="QueryOverride" type="string"><![CDATA[<OrderBy><FieldRef Name="Sorted1"
     Ascending="True"/><FieldRef Name="CDate" Ascending="False"/></OrderBy>]]></property>
Now that the data is in the correct order, only show the first record. First create a variable that contains the value of the unique field. Then use the variable to get a count of records before the current record that contains the unique value.
   <xsl:variable name="UniqueField" select="normalize-space(@Sorted1)" />
   <xsl:variable name="CountUniqueField"
        select="count(preceding-sibling::*[@*['Sorted1']=$UniqueField])" />     
Wrap your detail html with the following if statement.
<xsl:if test="$CountUniqueField &lt; 1">
     <!-- Your xslt & html here --->
</xsl:if>

Format as KPI/Stop Light/Green Yellow Red

Here the status field is compared to green/yellow/red and the src attribute of the <img> tag is set appropriately.
   <img>
      <xsl:attribute name="src">
         <xsl:if test="normalize-space(@status) = 'Green'">/images/green.gif</xsl:if>
         <xsl:if test="normalize-space(@status) = 'Yellow'">/images/yellow.gif</xsl:if>
         <xsl:if test="normalize-space(@status) = 'Red'">/images/red.gif</xsl:if>
      </xsl:attribute>
   </img>


It looks like this all put together

    <xsl:template name="CustomItemStyle" match="Row[@Style='CustomItemStyle']"
    mode="itemstyle">
       
        <xsl:variable name="SafeLinkUrl">
              <xsl:call-template name="OuterTemplate.GetSafeLink">
                   <xsl:with-param name="UrlColumnName" select="'LinkUrl'" />
              </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="DisplayTitle">
              <xsl:call-template name="OuterTemplate.GetTitle">
                <xsl:with-param name="Title" select="normalize-space(@Title)" />
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'" />
              </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="LinkTarget">_blank</xsl:variable>
        <xsl:variable name="UniqueField" select="normalize-space(@Sorted1)" />
        <xsl:variable name="CountUniqueField"
               select="count(preceding-sibling::*[@*['Sorted1']=$UniqueField])" />
         
        <xsl:variable name="HEADER">
            <xsl:if test="count(preceding-sibling::*)=0">
                 <![CDATA[<table border="0" cellspacing="0" width="100%">
                     <tr>
                       <th>Link To Item</th>
                       <th>Status</th>
                       <th>Date</th>           
                    </tr>
                 ]]>
           </xsl:if>
       </xsl:variable>

       <xsl:variable name="FOOTER">
           <xsl:if test="count(following-sibling::*)=0">
                <![CDATA[ </table> ]]>
           </xsl:if>
       </xsl:variable>

       <xsl:value-of select="$HEADER" disable-output-escaping="yes"/>
  
       <xsl:if test="$CountUniqueField &lt; 1">
           <tr>
              <td class="ms-vb">
                <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
                  <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}"
                   style="font-size:12px;font-weight:bold;">
                       <xsl:value-of select="$DisplayTitle" />
                  </a>
              </td>
                       
              <td class="ms-vb">
                 <img>
                     <xsl:attribute name="src">
                         <xsl:if test="normalize-space(@status) = 'Green'">
                            /images/green.gif</xsl:if>
                         <xsl:if test="normalize-space(@status) = 'Yellow'">
                            /images/yellow.gif</xsl:if>
                         <xsl:if test="normalize-space(@status) = 'Red'">
                            /images/red.gif</xsl:if>
                     </xsl:attribute>
                  </img>
              </td>

              <td class="ms-vb">
                 <xsl:value-of select="ddwrt:FormatDate(string(@CDate) ,1033 ,1)"/>
              </td>

           </tr>
  
       </xsl:if>

       <xsl:value-of select="$FOOTER" disable-output-escaping="yes"/>

    </xsl:template>

Thursday, April 22, 2010

Using SPList as Datasource for jQuery UI Autocomplete

A client recently wanted an auto complete field populated with a SharePoint list. Luckily, the jQuery UI library recently added an auto complete widget to their offering. So with the widget and SharePoint's ability to expose SPList data as xml we have a robust solution at our fingertips.

First, lets look at SharePoint's underutililized feature of exposing lists as xml through the url. That's right, use the right url and your list comes back as xml.  Here's the format

"#WEBURL#/_vti_bin/owssvr.dll?Cmd=Display&List={#LISTGUID#}&Query=Title%20Name&XMLDATA=TRUE"

Replace #WEBURL# with url of your site.
Replace #LISTGUID# with your list's guid.
The "Query=Title%20Name" limits the fields coming back to "Title" and "Name". These are the internal field names, one more reason not to use spaces in your field names.

These are really only the basics of what you can do with this techinque, the rest can be found here. URL Protocol

Next, you will need jQuery version 1.4.2 and jQuery UI version 1.8.0. You can either download these from their sites or use the Google cdn. Take a look here about adding the jQuery library and code into SharePoint if you need to. Use the code below and you have an input box with auto complete feed by a list. Pretty cool.

  <script type="text/javascript">     
           (function(){
                 var xmlSource = "#WEBURL#/_vti_bin/owssvr.dll?Cmd=Display&
                                  List={#LISTGUID}&Query=Title%20Name&XMLDATA=TRUE";

                 $.ajax({
                    url: xmlSource,
                    dataType: "xml",
                    success: function(xmlResponse) {
                        var data = $("z\\:row", xmlResponse).map(function() {
                            return {
                                title: $(this).attr("ows_Title"),
                                name: $(this).attr("ows_Name")
                            };
                        }).get();
                        $("#auto").autocomplete({
                            source: data,
                            minLength: 1,
                            select: function(event, ui) {
                                var title = ui.item.title;
                                var name = ui.item.name;
                           //TO DO what to do with the values that have been selected
                            }
                        });
                    }
                })//end .ajax

            }) //end function
  </script>

<input id="auto" type="text" />


This solution could be wrapped in a custom field control. If you were to do this you may be able to do away with lookup fields and provide auto complete everywhere in your site.

Wednesday, March 17, 2010

Too Many Users Remoted into Machine

There's a really easy way around the too many users remoted into the server. Just remember that you may be bumping someone else from the machine. An added bonus most of the time imo.

C:\>mstsc -v:MachinenameOrIP /F -admin

Wednesday, March 3, 2010

SharePoint TreeView Site Navigation

This is a really simple and powerful solution for site navigation within a site collection. It consists of a TreeView control, a PortalSiteMapProvider and a SiteMapDataSource. It sounds like there's a lot going on and there is, but SharePoint is taking care of most of the work for us.


The TreeView control gives a familiar look most users will quickly recognize. It can be easily styled  with css or skins.




Here is the editor of the webpart. The TreeView section has been added for a few customizations. The Site Map Provider is provided by SharePoint. Additional providers can be found in the web.config file. Number of levels to Show sets the number of levels to expand. Only Display Subsites if checked will only display the current site and it's subsites.

The code below creates and customizes the sitemapprovider.
        //setup the sitemapprovider
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            SiteMapProvider siteMapProvider = SiteMap.Providers[_siteMapProvider];
            if (siteMapProvider == null)
            { return; }

            InitPortalSiteMapProvider(siteMapProvider);
        }

        //set some defaults for the customized data provider
        //this is intended to only show sites and not pages
        private void InitPortalSiteMapProvider(SiteMapProvider siteMapProvider)
        {
            if (siteMapProvider is PortalSiteMapProvider)
            {
                _provider = siteMapProvider as PortalSiteMapProvider;
                _provider.DynamicChildLimit = 0;
                _provider.EncodeOutput = true;
                _provider.IncludePages = PortalSiteMapProvider.IncludeOption.Never;
                _provider.IncludeSubSites = PortalSiteMapProvider.IncludeOption.Always;
                _provider.IncludeHeadings = false;
                _provider.IncludeAuthoredLinks = false;
            }
        }

Here's the CreateChildControls method where everything is put together.
        protected override void CreateChildControls()
        {
            Controls.Clear();
            //create the datasource
            _datasource = new SiteMapDataSource();
            //associate the datasource with the customized provider
            _datasource.Provider = _provider;
            //if true only show self and subsites
            _datasource.StartFromCurrentNode = startAtCurrentWeb;

            treeView = new TreeView();
            treeView.ExpandDepth = levels;
            //set the datasource of the treeview and bind it
            treeView.DataSource = _datasource;

            treeView.DataBind();
      
            Controls.Add(treeView);
        }

Monday, February 22, 2010

PowerShell ISE Server 2008 R2

By default PowerShell ISE is not available in Server 2008 R2. It's a feature that you will need to add. Go to Server Manager -> Features -> Add Features

PowerShell uses the "profile" concept somewhat similar to Unix. Profiles can be really useful in setting up your PowerShell session defaults. There are a number of built in profiles I was expecting to be available and was surprised when I had to create one.
   
How to use Profiles in Windows PowerShell ISE
How to Create Profiles in Windows PowerShell ISE

Create a new Profile
if (!(test-path $profile.CurrentUserAllHosts)) 
{new-item -type file -path $profile.CurrentUserAllHosts -force}

Add the SharePoint Snapin and run all my commands without annoyance. Execute the psEdit command and add the Add-PSSnapin and Set-Executionpolicy commands in the tabbed window at the top. You'll have to save the changes.
psEdit $profile.CurrentUserAllHosts
Add-PSSnapin Microsoft.SharePoint.Powershell
Set-ExecutionPolicy Bypass