If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Wednesday, November 9, 2011

Remove query string from url

I needed to delete the querystring value from a Url. The code I used to do this is right at the end of this post.

First though, here's the reason I needed to do it.

I found that if you call the SPWeb.GetFile method, with a Url that contains a query string, the value of Item for the returned SPFile will be null. Here's an example:

string url = "http://red-hb:8080/TestSite/Pages/Tamar.aspx?x=y";

using (SPSite site = new SPSite(url))
using (SPWeb web = site.OpenWeb())
SPFile file = web.GetFile(url);
Console.Write(file.Item["Title"]); // Item will be null.

OK, here's how I removed the query string:

string url = "http://red-hb:8080/TestSite/Pages/Tamar.aspx?x=y";

Uri uri = new Uri(url);

string queryString = uri.Query;

if (String.IsNullOrEmpty(queryString) == false)
url = url.Replace(uri.Query, String.Empty);

Thursday, November 3, 2011

"This solution contains no resources scoped for a Web application"

I created a SharePoint Solution that contained a single Feature. The Feature had a Receiver which is used to make web.config modifications.

I wanted to target these modifications at specific SharePoint web applications. However, when I ran the following PowerShell command, I got the message: "This solution contains no resources scoped for a Web application".

Install-SPSolution –Identity <WSP NAME>.wsp –WebApplication http://<TARGET APP URL> -GACDeployment -Force

Other people suggest not including the target web application url. I don't want to do this as it will affect all web applications, not just our own.

The workaround I used was to include the Solution assembly within its own package, and mark it as a "SafeControl".

I realise that this not ideal. Do you have a better solution?

The steps I followed were:

1) Open the Project Properties for the Solution, and set "Include Assembly in Package" to "False".

Include Assembly in Package

2) Edit the Package manifest and include the assembly from the project's own output. Add the assembly / namespace as a SafeControl.