Archive

Archive for the ‘Development’ Category

SharePoint custom search application – sorting by managed property

If sorting by a managed property, the managed property needs to have the option “Reduce storage requirements for text properties by using a hash for comparison.” checked.

image

If you try to sort by a managed property that does not have this value, an exception will be thrown.  Bad exception! Bad!

Categories: Development, SharePoint Tags: ,

VS2010: Changing the SharePoint wsp file name

June 30, 2010 1 comment

The default file name given to your solution will match the project name.  I.e. a SharePoint project SharePointProject1 will result in a package SharePointProject1.wsp.

If you want VS to spit out a different name, modify the name attribute in the Package\Package.package file for your project.

Code Snippet
  1. <?xml version=1.0 encoding=utf-8?>
  2. <package xmlns:dm0=http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core dslVersion=1.0.0.0 Id=93b1d26c-eca7-4dc6-8a0e-6e96bc980c63 solutionId=93b1d26c-eca7-4dc6-8a0e-6e96bc980c63 resetWebServer=false name=SharePointProject1 xmlns=http://schemas.microsoft.com/VisualStudio/2008/SharePointTools/PackageModel>
  3. <features>
  4. <featureReference itemId=896bdcb0-4dd8-428a-a028-77e509bc9108 />
  5. </features>
  6. </package>

You may need to restart VS after making this change.

Renaming your SharePoint web part namespace

June 30, 2010 3 comments

So it’s often the case that I start a project and then decided later I want to refactor such elements like the namespace, package name etc.  Let’s start with the namespace.

Project Namespace

When you create a new project (lets start with an empty solution), you provide the project name, solution name, and location.

Project: SharePointProject1

Solution: SharePointSolution1

image

Great.  We have our solution.  We add a web part item to the project and we get:

image

Before we go any further, lets build and deploy.  Everything looks good according to the web part preview from the web part gallery.

image

Now we want to change the following:

  • Solution Name
  • Namespace
  • Project Name
  • Assembly Name

Renaming the solution, project and files in Solution explorer, and the assembly name through project properties should not cause a problem.  But when we change the namespace in the web part code (and update the web part file), we get the following error:

image

This typically indicates a problem between the web part file, the assembly, and the SafeControl entry in web.config.

If we look at the deployed web part file we see:

.webpart
  1. <?xml version=1.0 encoding=utf-8?>
  2. <webParts>
  3. <webPart xmlns=http://schemas.microsoft.com/WebPart/v3>
  4. <metaData>
  5. <type name=contoso.webparts.SampleWebPart, contoso.webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b896b8322cc3e164 />
  6. <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  7. </metaData>
  8. <data>
  9. <properties>
  10. <property name=Title type=string>WebPart1</property>
  11. <property name=Description type=string>My WebPart</property>
  12. </properties>
  13. </data>
  14. </webPart>
  15. </webParts>

If we look at the web.config for the safe control entry:

Web.Config
  1. <SafeControl Assembly=contoso.webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b896b8322cc3e164 Namespace=SharePointProject1.WebPart1 TypeName=* Safe=True SafeAgainstScript=False />

Notice the SafeControl entry is incorrect for Namespace.  In order to correct this, the file SharePointProjectItem.spdata (located in the web part folder in windows explorer) needs to be updated.  SharePoint does not update this file for you.

SharePointProjectItem.spdata
  1. <?xml version=1.0 encoding=utf-8?>
  2. <ProjectItem Type=Microsoft.VisualStudio.SharePoint.WebPart DefaultFile=SampleWebPart.cs SupportedTrustLevels=All SupportedDeploymentScopes=Site xmlns=http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel>
  3. <Files>
  4. <ProjectItemFile Source=Elements.xml Target=SampleWebPart\\cf0 ” Type=ElementManifest />
  5. <ProjectItemFile Source=SampleWebPart.webpart Target=SampleWebPart\\cf0 ” Type=ElementFile />
  6. </Files>
  7. <SafeControls>
  8. <SafeControl Name=SafeControlEntry1 Assembly=$SharePoint.Project.AssemblyFullName$ Namespace=SharePointProject1.WebPart1 TypeName=* IsSafe=true IsSafeAgainstScript=false />
  9. </SafeControls>
  10. </ProjectItem>

Notice the SafeControl entry above where the namespace value is incorrect.  Change this to the correct namespace and re-deploy.  Now we’re back in business!