Archive

Archive for the ‘Development’ Category

SharePoint Development: Speed up development with Visual Studio CKSDEV Keyboard Shortcuts

February 19, 2014 Leave a comment

Introduction

Firstly if you are not using the CKSDEV Visual Studio extensions for SharePoint. Then please do yourself a favour and download them NOW!

Wes Hackett and the team have been doing an amazing job to save us time when developing SharePoint solutions.

You can download the Visual studio extension via Visual Studio’s gallery.

Keyboard Shortcuts

I have to say that I was a little late to the 1.2 release which I updated to in November even though it was out in August! Anyway this release is the best yet for SharePoint 2013 and Visual Studio 2012. Finally it includes a SharePoint solution deployment profile to update/upgrade your solutions. Finally, I can throw away my version!

Anyway, I noticed that there were lots more keyboard shortcuts (I am pretty sure these have been there for a while) but I wanted to start making use of them as it really makes life much easier.

Fortunately the CKSDEV development team have a strategy for the shortcut conventions so remembering them is made a little easier.

To recycle a process then the two stage shortcut will always use Alt+R

To attach the debugger to a process then the two stage shortcut will always use Alt + A

So here are the shortcuts which I use the most:-

  • Recycle SharePoint Application Pools
    • Alt+R, A
  • Recycle SharePoint Timer Service
    • Alt R. T
  • Attach debugger to SharePoint Application Pools
    • Alt A, S
  • Attach debugger to SharePoint Timer Service
    • Alt A, T

    This makes life so much easier when you are in the code, debug, fix, code, debug, fix cycle!

Feature Upgrade Issue: Deleted SPWebs : An error occurred while enumerating through a collection

July 4, 2012 Leave a comment

Introduction

Recently I published a post about issues when upgrading Site Scope features in SharePoint. The issue was related to deleted Site Collections which were still in the recycle bin.

After resolving this issue I decided to do some testing with other deleted SharePoint objects. The next logical object type to delete was an SPWeb object.

Unfortunately further issues were found and the following section explains how they were found and the approach to resolve them.

 

How the Issue was Approached

The approach taken was the following:-

  • Build a Web Scoped Feature marked v1.0.0.0
  • Install the SharePoint Solution
  • Create a number of Webs and Activate the feature on those webs
  • Delete one of the Webs
  • Update the Web Scoped Feature to v2.0.0.0
  • Upgrade the SharePoint Solution to deploy the new version of the Web Scoped Feature
  • Use Install-SPFeature [FeatureName] –Force to upgrade the feature
  • Run a PowerShell command to execute the SPSite.QueryFeatures() function

The following PowerShell was run to query for Features that need to be upgraded.

 $site=Get-SPSite http://sharepoint; $site.QueryFeatures("Web", $true); 

The result of the script displayed the following:-

 $site.QueryFeatures("Web",$true);  An error occurred while enumerating through a collection: Unable to access web scoped feature (Id: f41cc668-37e5-4743-b4a8-74d1db3fd8a4) because it references  a non-existent or broken web (Id: 3bd828cf-b747-4111-b888-5953b0d9aaa3) on sit e 'http://dev.itsp.local'.  Exception: System.ArgumentException: Value does not  fall within the expected range.    at Microsoft.SharePoint.SPWebCollection.get_Item(Guid id)    at Microsoft.SharePoint.SPFeatureEnumeratorBase.GetCachedWeb(SPSite site, Gu id webId, Guid featureId). At line:1 char:20 + $site.QueryFeatures >> ("Web",$true)     + CategoryInfo          : InvalidOperation: (Microsoft.Share...esultCollec    tion:SPFeatureQueryResultCollection) [], RuntimeException     + FullyQualifiedErrorId : BadEnumeration 

 

Solution

The approach to solving this issue was not to write a workaround but to run a script which checks for deleted SPWeb objects in the site collection.

 $site = Get-SPSite http://sharepoint; $site.RecycleBin | ?{$_.ItemType -eq "Web"}; 

The script will display the SharePoint Web Objects that have been deleted and are still found in the site collections recycle bin.

The deleted SPWeb objects can be removed from the recycle bin using the following PowerShell command.

 $site = Get-SPSite http://sharepoint;  $deletedWebs = $site.RecycleBin | ?{$_.ItemType -eq "Web"}; $deletedWebs | % {$site.RecycleBin.Delete($_.Id)} 

This will delete the SPWeb objects from the recycle bin and the Feature Upgrade process can continue.

Please be careful when running this script! Only run it when you know that none of the SharePoint Web sites are not needed!

 

Anyway hope that helps!

Useful Visual Studio External Tools

June 6, 2012 Leave a comment

Introduction

I have been meaning to document this for a while. Every time I set up a new machine I have to check the configuration of my Visual Studio external tools.

External Tools? If you don’t know what I am talking about then take a look at the image below.

image

Currently I have the following External Tools:-

  • Create GUID – this allows me to quickly get a new GUID when creating all those SharePoint resources, including Content Types, List Schemas, Feature Receivers etc.
  • Get Public Key Token – this uses the .Net Framework Signing Tool (sn.exe) to get the Public Key token, although with the new SharePoint tools in Visual Studio this isn’t needed so much there are always those odd occasions.
  • Get Full Assembly Name – This is used lots particularly when having to hack together various SharePoint Xml files or Entity Framework resource connection strings. I can’t take credit for this one, Sahil Malek – http://blah.winsmarts.com/2009-12-SharePoint_Productivity_Tip_of_the_day.aspx), did all the hard work.
  • Get IIS Web Application Information – This saves me so much time when debugging code especially, feature receivers, application page, web part code. The tool will display a list of all the Web Applications and there Process ID (PID).

Setting up the Tools

Create GUID Tool

First of all locate the Guidgen Tool which should be found in your Windows SDK folder. The path will depend on the Windows Version you are running, this one is for Windows 2008 R2. “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\”

  • The GUID Generator application can be found in the bin subfolder
  • Full Path: “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\guidgen.exe”
  • Now you have the path, fire up Visual Studio

clip_image002

  • Click Tools->External Tools
  • Click Add
  • Title: Generate GUID
  • Command: : “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\guidgen.exe”
  • Click Apply

clip_image004

When this tool is used then the following window will popup:-

clip_image006

Get Public Key Token Tool

This retrieves the public key token for the assembly that is associated to the file that is open in Visual Studio.

To setup the tool do the following:-

  • Get the Path to the SN.exe tool which is found in the .Microsoft SDK Path
  • “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe”
  • From Visual Studio
  • Click Tools->External Tools
  • Click Add
  • Title: Get Public Key Token
  • Command: : “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe”
  • Attributes: -Tp $(TargetPath)
  • Check “Use Output Window”
  • Click Apply

clip_image008

When the External Tool is used then the following output will appear in Visual Studio’s Output Window

clip_image010

Get Full Assembly Name Tool

This tool is excellent and saves me lots of time when you need the full Assembly Name for all the numerous SharePoint development tasks.

The setup of the tool comes from Sahil Malek (http://blah.winsmarts.com/2009-12-SharePoint_Productivity_Tip_of_the_day.aspx).

  • Fire up Visual Studio
  • Click Tools->External Tools
  • Click Add
  • Title: Get Full Assembly Path
  • Command: PowerShell
  • Arguments: -command “[System.Reflection.AssemblyName]::GetAssemblyName(\”$(TargetPath)\”).FullName”
  • Check “Use Output Window”
  • Click Apply

clip_image012

The output for this tool is as follows:-

clip_image014

Get IIS Web Application Info

This tool is great for getting information on the Web Server Application Pools. This is great if you need to debug a particular Web Application Pool Process.

  • Fire up Visual Studio
  • Click Tools->External Tools
  • Click Add
  • Title: Get IIS WP Info
  • Command: “c:\windows\system32\inetsrv\appcmd.exe”
  • Arguments: list WP
  • Check “Use Output Window”
  • Click Apply

clip_image016

The Output for this tool is as follows:-

clip_image018

Conclusion

I hope you find some of those tools useful, I would love to hear about any that you use.

Introducing SPUrlExpressionBuilder to resolve SharePoint URL Tokens

November 29, 2011 Leave a comment

Introduction

One of the great things when developing with SharePoint is being able to use URL tokens such as:-

  • ~SiteCollection – this will resolve to the current site collection root
  • ~Site – this will resolve to the current web root

For an extensive list of SharePoint URL tokens, take a look at Namwar Rizvi’s SharePoint Insight blog post, http://sharepoint-insight.com/2008/12/01/list-of-sharepoint-url-tokens/.

If you have done much with creating or modifying Master Pages or Page Layouts then these tokens become very useful to locate resources such as images, XSL or CSS files.

For example, to reference an image which is stored in a SharePoint Publishing web’s Site Collection Images folder you could use something like this:-

~SiteCollection/SiteCollectionImages/myimage.png.

Recently, I have been working with multiple language user interfaces and wanted to be able to display a different image based on the language. One of the steps to do this was to work out how to process these URL Tokens.

Solution

This is where the SPUrlExpressionBuilder class comes to the rescue. This class is part of the Microsoft.SharePoint.Publishing.WebControls assembly.

To evaluate a string with a url token like ~SiteCollectionUrl/Images/MyImage.png you can use the following SPUrlExpressionBuilder class’s method EvaluateUrlExpression().

string imagePath = SPUrlExpressionBuilder.EvaluateUrlExpression("~SiteCollection/Images/myimage.png").ToString();

 

Visual Studio Tip: Using SharePoint Site Url in Pre-Deployment and Post-Deployment scripts.

October 14, 2011 2 comments

Within Visual Studio 2010 SharePoint projects you might want to use the SharePoint Site Url that you have specified for your SharePoint project in Pre or Post Deployment scripts.

The SharePoint Site Url value can be retrieved using the $(SharePointSiteUrl) variable.

Usage

For example, this variable could be used in a pre-deployment script to deactivate certain features for the debug configuration.

To add the Pre Deployment script do the following:-

  • From Visual Studio 2010
  • Open your SharePoint Solution/Project
  • Click Project Properties
  • Click the SharePoint Tab
  • Add the following script to the Pre Deployment Script text box.
echo SiteUrl: $(SharePointSiteUrl)
if "$(ConfigurationName)" == "Debug" (
stsadm -o deactivatefeature -name "Feature1" -url $(SharePointSiteUrl) -force
stsadm -o deactivatefeature -name "Feature2" -url $(SharePointSiteUrl) -force
stsadm -o deactivatefeature -name "Feature3" -url $(SharePointSiteUrl) -force
stsadm -o deactivatefeature -name "Feature4" -url $(SharePointSiteUrl) -force
stsadm -o deactivatefeature -name "Feature5" -url $(SharePointSiteUrl) -force
)

Hope that helps.

For more information on Pre and Post-Deployment steps see this MSDN Link:

How to: Set SharePoint Deployment Commands (http://msdn.microsoft.com/en-us/library/ee231534.aspx)

 

U4FVW5JMFNEH

Visual Studio SharePoint Packaging Issue: Both "SharePointItem" and "SharePointItem" contains a file that deploys to the same Package location.

October 14, 2011 2 comments

I have had this problem a few times when moving and copying SharePoint SPI objects within Visual Studio 2010.

When you attempt to package and deploy your SharePoint solution the following error occurs:-

  • C:\TFSBulid\4\Project\BuildLocation\Sources\ProjectName\Package\Package.package: Both “[ExampleSiteDefinition]” and “[ExampleSiteDefinition]” contain a file that deploys to the same Package location: [Location]

The problem occurs because two of the SharePoint Project Item (SPI) have references to the same location. This seems to occur when you copy SharePoint Project Items.

If you take a look at the SharePointProjectItem.spdata file for the SharePoint Project Item that you copied from the SharePoint Point Item specified in the [Location] portion of the error message you will see that the SharePointProjectItem.spdata file will show details of the old SharePoint Project Item rather then the new SharePoint Item.

The content of the SharePointProjectItem.spdata looks like this:-

 1: <?xmlversion="1.0"encoding="utf-8"?>
 2: <ProjectItemType="Microsoft.VisualStudio.SharePoint.SiteDefinition" DefaultFile="onet.xml" SupportedTrustLevels="FullTrust" SupportedDeploymentScopes="Package"
 3: xmlns=http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel>
 4: <Files>
 5: <ProjectItemFileSource="default.aspx" Target="SiteTemplates\ExampleSiteDefinition\"
Type="TemplateFile" />
 6: <ProjectItemFileSource="onet.xml" Target="SiteTemplates\ExampleSiteDefinition\Xml\"
Type="TemplateFile" />
 7: <ProjectItemFileSource="webtemp_ExampleSiteDefinition.xml" Target="1033\XML\"
Type="TemplateFile"/>
 8: </Files>
 9: </ProjectItem>

To fix this update the ProjectItemFileSource element’s Target attribute to point to the correct location.

Creating List Instance: Object Reference not set to an instance of an object

September 12, 2011 4 comments

Introduction

This one is a bit of an old issue but some of my posts are just to remind me of a time that I got stuck in the past. Also if it helps someone else then great.

It is possible to create a feature which creates an instance of a SharePoint List when its activated. However, sometimes you only want a list to be created on the Root Web of a site collection.

Issue

Well the XML for specifying a ListInstance as a feature has an attribute called RootWebOnly which I thought I could use to only allow the ListInstance to be created on the RootWeb.

Unfortunately when you do that you get a message like the following in your SharePoint ULS Logs when the feature is activated:-

Feature Activation: Threw an exception, attempting to roll back. Feature ‘FeatureName’(ID: ‘xxxxxxxx-xxxx-xxxx-xxxxxxxx). Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.SPListInstanceElement.ElementActivated(SPFeaturePropertyCollection props, SPSqlCommand sqlcmdAppendOnly, SPWebApplication webApp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionListInstances(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.SPFeature.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce)

The offending Feature ListInstance element is below:-

 1: <?xmlversion="1.0" encoding="utf-8"?>
 2: <Elements
 3: xmlns="http://schemas.microsoft.com/sharepoint/">
 4: <ListInstance
 5: Id="AListId"
 6: Title="A List"
 7: Description="A List 
 8: Description”
 9: Url="Lists/ListUrlHere"
 10: OnQuickLaunch="FALSE"
 11: RootWebOnly=”TRUE”
 12: FeatureId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
 13: TemplateType="45100"></ListInstance>
 14: </Elements>

Solution

To fix the feature activation remove the RootWebOnly=”TRUE” from your ListInstance element manifest file and instead move the RootWebOnly=”TRUE” into your List’s schema.xml.

To ensure that the list is only provisioned at the root web of the site collection change the Feature.xml to use Scope=”Site”

Update:

Thanks to Dennis who picked up my mistake in this post which stated that the RootWebOnly attribute should be moved to the feature.xml.

%d bloggers like this: