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>

Advertisements

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.

Alternate Access Mappings – 2010

Sharepoint 2010 Issue

With the introduction of the client API, and a few other services, Microsoft is now making use of the WCF for its web services.   These offer lots and lots of advantages,  but have one limitation (at least in 3.5) that makes the above scenario fail.

You cannot have multiple bindings on any IIS website

http://www.21apps.com/sharepoint-2010/sharepoint-2010-and-multiple-urls/