SharePoint Content Query Web Part (CQWP) Toolset

The CQWP is a fantastic tool built into SharePoint Sever edition which allows an admin to retrieve items from lists within a site collection. What makes it powerful is its out of the box functionality to style the items retrieve using XSLT & CSS. This article provides some common toolsets that use on a common basis.

There are some great blogs around that provide instructions on how to use this web part so Im not going to do that again here. This is simply a cheat sheet when using the CQWP. If you are new to this web part on want to learn the basics I recommend this article by Paul Galvin

CommonViewFields – Types

<property name="CommonViewFields" type="string">InternalColumn1, FieldType; InternalColum2, FieldType;</property>

  • Text
  • Note
  • Number
  • Currency
  • Integer
  • Boolean
  • DateTime
  • Threading
  • Lookup
  • Choice
  • URL
  • Counter
  • DisplayOnly (DisplayOnly field type has no storage of its own)
  • RichHTML
  • Image

Available Fields For Use

To iterate through all of your available fields use the following:

    <xsl:for-each select="@*">
      P:<xsl:value-of select="name()" /><br/>
    </xsl:for-each>

Retrieve Value of Field

    <xsl:value-of select="@Description" />

Force tag to render

An empty html tag will be stripped out if it contains no content. For example if you want to render <div class=”clear”></clear> this would be stripped out. The easiest way I have found to ensure it renders is to use the following:

<xsl:text> </xsl:text>
<div class="clear" ><xsl:text> </xsl:text></div>

Format URL to get Display Text & URL

<a href="{substring-before(@Link1,’,’)}"><xsl:value-of select="substring-after(@Link1,’, ‘)"/></a>

Render a list of Links

 

<xsl:template name="QuickLinks" match="Row[@Style='QuickLinks']" mode="itemstyle">
   <xsl:param name="CurPos" />
   <xsl:param name="Last" />
   <xsl:variable name="tableStart">
      <xsl:if test="$CurPos = 1">
         <![CDATA[
            <ul id="quickLinkList">
            ]]>
      </xsl:if>
   </xsl:variable>
   <xsl:variable name="tableEnd">
      <xsl:if test="$CurPos = $Last">
         <![CDATA[ </ul>  ]]>
      </xsl:if>
   </xsl:variable>
   <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="SafeImageUrl">
      <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
         <xsl:with-param name="UrlColumnName" select="'ImageUrl'"/>
      </xsl:call-template>
   </xsl:variable>
   <xsl:variable name="DisplayTitle">
      <xsl:call-template name="OuterTemplate.GetTitle">
         <xsl:with-param name="Title" select="@Title"/>
         <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
      </xsl:call-template>
   </xsl:variable>
   <xsl:value-of select="$tableStart" disable-output-escaping="yes"/>
   <div class="QLitem">
      <div class="link-item">
         <a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
            <xsl:value-of select="$DisplayTitle"/>
         </a>
      </div>
   </div>
   <xsl:value-of select="$tableEnd" disable-output-escaping="yes"/>
</xsl:template>

Developing SharePoint 2007 Webparts using Visual Studio 2010

Today I had a client that came to me that liked a web part that I had developed and wanted me to install it for them with some slight mods. No problem… Ohh wait, we developed that for SharePoint 2010 using Visual Studio 2010.

I am a huge fan of Visual Studio 2010 and its tight integration with SharePoint 2010 in its ability to quickly create features and write code like its supposed to be written (without the major headache!). Although WSPbuilder was the tool of chose for SP 2007, VS 2010 is now my new favourite weapon. With new functions like creating Visual Web Parts in a couple of clicks, features, deploy & debug with F5,  control over deployment and much more. Its fantastic, well done to Microsoft for making what should be a simple job easy again!

Since the release of SharePoint 2010 I now do a lot my development in SharePoint 2010 even if my client is running on SharePoint 2007. Why? Well because the API’s for SP 2007 & 2010 are very similar, and really haven’t effected much of my development to date. I find it so much easier to development in SP 2010 with Visual Studio 2010 especially when I run Windows 7 64-bit with SharePoint Foundation 2010 installed. Having this setup means that i can deploy directly from my local PC to a SharePoint environment for basic testing. No need to manually create project structure, manifiest files, signing of assembly, building wsps, manually deploying, attach to debug etc. Once I’m all done with development I package it up and deploy the wsp to the Sharepoint 2007 development for thorough testing. Provided I haven’t used any deprecated methods in the API, ill be all sweet. Good thing is if you have used and deprecated methods you wont be able to build solution, so you wont be doubt when deploying to SP 2007.

Problem.

So i presume your reading this because you love the new Visual Studio 2010 but need to develop web parts for SharePoint 2007. Although the interface within Visual Studio doesn’t directly enable you to develop for 2007, it is possible with some minor changes to your project.

So for the purposes of this article I am going create a basic visual web part, something that was difficult to do in SharePoint 2007 development. This process should work for most project types available…

  1. Start New Project, select Visual Web Part
    image
  2. Select – Deploy as a farm solution
    image
  3. Write your custom web part. I have just created a simple web part that contains some text and a button. When the user clicks the button the button text will change.
    image
  4. I now press F5 on the keyboard and Visual Studio will automatically build & deploy my web part to SharePoint 2010.
  5. I confirm it all works by adding the web part to the page. Great! But there is nothing special so far.
    image
  6. Now to get it into SharePoint 2007! First lets try deploying the current wsp as it is…
  7. So we need to package up the solution.
    image
  8. Grab the wsp from the bin folder
  9. Copy to SharePoint 2007 server.
  10. Deploy to 2007 env and we get an error. The error tells us that it found a version number in the manifest that it doesnt understand.
    image “Solution manifest for solution ’48cae4c9-68ba-cccf5a89304′ failed validation, file manifest.xml, line 2, character 110: The ‘SharePointProductVersion’ attribute is not declared.

Solution

After the steps above:

  1. Go back into Visual Studio 2010.
  2. Double click the “Package.package” file
  3. Select “Manifest” down the bottom. Notice where is reads SharePointProductVersion=”14.0” in the xml file.
    image
  4. In the properties panel, clear out 14.0 in the SharePointProductVersion field. This removes the property all together from the manifest file.
    image
  5. We also need to change the assembly reference in the project from Microsoft.SharePoint.dll 14.0 to 12.0.
    To do this you will need to click the show all files icon in the solution explorer panel.  image 

    -Next, delete out Microsoft.SharePoint & delete out Microsoft.SharePoint.Security as these wont be on the SP 2007 environment.-Then you want to add a reference to Microsoft.SharePoint.dll but make sure its the 12.0 version found in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll

  6. Because we have built a ‘Visual Web Part’, Visual Studio 2010 automattically adds some imports into the ascx user control file which wont exists in SP 2007. Therefore you need to comment these references out. Obviously if we were doing a coded web part or a event receiver we would not need to do this step.Mine currently looks like this.
    image
  7. Package up your solution again and deploy on SP 2007 environment.
    image
  8. Active, test and your good to go.
    SharePoint 2007 webpart

Notes:

  • You will need Asp.Net 3.5 installed on your SharePoint 2007 environment.
  • This may not always be the right solution for you (depending on your project) but it certainly saves me a lot of time for those basic web parts.

the list cannot be displayed in datasheet view for one or more of the following reasons

For some very dumb strange reason Microsoft forgot about SharePoint datasheet view when developing Office 2010 64bit. I recently installed Windows 7 x64bit with Microsoft Office 2010 64bit. Now there are many people out there who dont recommend installing Office 2010 64bit (install 32bit instead) for various reasons but Microsoft continues to push us to 64 bit so why shouldn’t we make the most of costly Ram capacity.

 

If you are like me and have installed Office 2010 64 bit and utilise SharePoint 2007/2010 on a regular basis then you are bound to run into a very weird error. This error comes up when trying to edit a list in “datasheet view”, a common practice in SharePoint for bulk data management. Unfortunately, Microsoft forgot about this module and doesn’t install the required ActiveX object that comes with Office 2007 and Office 20010 32 bit.

 

To resolve this issue you simply need to download and install the link below. This basically installs the required files for Active X to run, storing them in the x86 programs files folder.

 

Please leave a comment if you run into this issue as well.

 

http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

Sharepoint 2010 – Basic Powershell Commands

Below are the basics for installing sharepoint 2010 webparts using powershell.

 

  1. Open “Sharepoint 2010 Management Shell”
    image
  2. Add-SPSolution c:\wsps\contactus.wsp
  3. install-spsolution –identity contactus.wsp –webapplication http://moss-server –GACDeployment

    note: –GACDeployment is required if you are deploying to the assembly
  4. To update an existing web part you can run the following line
    Update-SPSolution –Identity SharePointProject2.wsp –LiteralPath c:\wsps/contactus.wsp –GACDeployment

Dont forget to activate your feature!

 

You can also add the following line to your ps1 files:

Add-PSSnapin “Microsoft.SharePoint.PowerShell”

This prevents the need for using SharePoint 2010 Management Shell. Just use normal powershell.

 

Installing Sharepoint 2010 on Windows 7 x64

The process for of installing sharepoint 2010 on windows 7 is rather simple unless you run into some issues as I did.

 

  1. Download or have ready sharepoint 2010.
    • On my development environment I just run sharepoint 2010 foundation server which can be download here.
  2. Then you want to follow the instructions found at the MSDN site http://msdn.microsoft.com/en-us/library/ee554869.aspx
  3. This all went pretty smoothly for me until I run the psconfig (Sharepoint Configuration Wizard) where I hit an error

An exception of type System.Security.Cryptography.CryptographicException was thrown. Additional exception information: The data is invalid.

Turns out that I needed to give my “Network Service” account full permissions to the 14 hive found at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14.
However this didn’t resolve the issue straight away and that’s when i found Bill Baer’s comment on the TechNet forum which says the following

Right click on the folder (‘%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14), select the Security tab, click Edit, click Add, as the user type “Network Service”, then click OK and apply. Grant the user Full Control. This will work on its own if you do it before running PSConfig.
If you’ve already hit the error, you may have to run psconfig –cmd configdb disconnect then delete the old configuration database from your SQL instance, then add the ACL and re run PSConfigUI.

After I had disconnected the database and manually deleted the config databases through Management Studio I was able to re-run the psconfig successfully.

Finally – Sope Website Goes Live!!

Its been a long time coming but finally my business, Sope Web Technologies, website has gone live, http://sope.com.au! With all our time focused on our customers it been hard to find spare time to focus on our own website…Please check out the website and let us know your feedback.

 

The website is built on top of the best CMS and collaborative platform currently available, Microsoft SharePoint 2010. This was our first real crack at 2010 so there were a few things we learnt which I will share in another blog post. SharePoint 2010 brings some huge enhancements from 2007 and it was very exciting to deep dive into this new functionality. I look forward to working with 2010 with our clients in the future!

Sharepoint SmartPart – ToolParts

Today I was creating a web part through means of asp.net usercontrol and the infamous SmartPart for Sharepoint 2007 and realized I hadn’t created a user control that allows properties using the toolpart model. I have used SmartPart on many occasions but normally prefer creating “real” web parts from scratch as it’s what I’m used to when I need to allow admins to “Modify Shared Web Part” and specify properties. However for this scenario I wanted to use smart part but after some Googling I couldn’t find anyone who had documented how to properly implement a toolpart (at least one I could understand J). So here goes, I hope this helps someone….

So my scenario is that I need a url from the user which will populate the hyperlinks within the web part itself as this web part can be installed on many different pages/sites/site collections etc.

First you have your web part class itself

demoWP.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="demoWP.ascx.vb" Inherits="SmartPartDemo.demoWP" %>
MY HTML HERE

<br />

<br />

demoWP.ascx.vb

Imports SmartPart
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebPartPages

Partial Public Class demoWP
    Inherits System.Web.UI.UserControl
    Implements SmartPart.IAdvancedUserControl

#Region "ToolPart"

    Dim mypart As SmartPart.SmartPart
    Private _strURL As String

    <ComponentModel.Browsable(False), ComponentModel.DefaultValue(0), WebPartPages.WebPartStorage(WebPartPages.Storage.Shared)> _
Property strURL() As String
        Get
            Return _strURL
        End Get
        Set(ByVal Value As String)
            _strURL = Value
        End Set
    End Property

    Public Function GetCustomToolParts() As Microsoft.SharePoint.WebPartPages.ToolPart() Implements SmartPart.IAdvancedUserControl.GetCustomToolParts
        Dim toolParts(1) As WebPartPages.ToolPart
        Dim MyTp As New toolpart1
        MyTp.Title = "Custom Properties"

        toolParts(0) = MyTp
        Return toolParts
    End Function

    Public Sub SetContext(ByVal webpart As SmartPart.SmartPart) Implements SmartPart.IAdvancedUserControl.SetContext
        mypart = webpart
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim url As String = mypart.GetCustomProperty("strURL")

        Dim lit As New Literal
        lit.Text = "<a href='" + url + "?id=xxx'>My Link</a>"
        Me.Controls.Add(lit)

    End Sub

End Class

Then the toolpart code file

toolpart1.vb

Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.SharePoint

Public Class toolpart1 : Inherits Microsoft.SharePoint.WebPartPages.ToolPart

    Dim MyPart
    Dim lbl As Literal
    Dim manualsurl As TextBox
    Dim li As ListItem

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyPart = Me.ParentToolPane.SelectedWebPart
    End Sub

    Protected Overrides Sub CreateChildControls()
        CreateControls()
    End Sub

    Public Overrides Sub ApplyChanges()
        EnsureChildControls()
        GetControlValues()
        MyPart.RefreshPropertyValues()
        MyPart.SaveWebPartProperties = True
    End Sub

    Public Overrides Sub SyncChanges()
        ApplyChanges()
        ApplyChanges()
        MyBase.SyncChanges()
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
        EnsureChildControls()
        PopulateControlValues()
    End Sub

    Protected Overrides Sub RenderToolPart(ByVal output As System.Web.UI.HtmlTextWriter)
        EnsureChildControls()
        RenderSelectReportDropDownListControls(output)
    End Sub

    Private Sub CreateControls()

        lbl = New Literal
        lbl.Text = "URL: "
        Controls.Add(lbl)

        manualsurl = New TextBox
        manualsurl.ID = "URLTxt"
        Controls.Add(manualsurl)
    End Sub

    Private Sub GetControlValues()
        MyPart.SetCustomProperty("strURL", manualsurl.Text)
    End Sub

    Private Sub PopulateControlValues()
        manualsurl.Text = MyPart.GetCustomProperty("strURL")
    End Sub

    Private Sub RenderSelectReportDropDownListControls(ByRef output As System.Web.UI.HtmlTextWriter)
        output.Write("</br>")
        manualsurl.RenderControl(output)
    End Sub

End Class

For this to all work you need to be creating a ‘web application’ which once built will give you a dll file which will go into your Sharepoint bin folder.

You should now notice a textbox in the toolpart pane allowing you to specify the url under ‘Custom Properties’.

Please leave a comment below if this has helped you out or you have found a better way!

References

http://smartpart.codeplex.com/Thread/View.aspx?ThreadId=15243