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.

Friday, October 28, 2011

SharePoint Anonymous Access

I was trying to change the Anonymous Access settings for a list, as per this screen:


SharePoint Anonymous Access checkboxes

However, when I checked the "Add Items" box and clicked OK, it did not save the changes. Everytime I repeated this and went back to the screen, the "Add Items" box was not checked.


The cause of the problem was that the Anonymous Policy for the SharePoint Application (that hosted the site) had been changed. By default this is set to "None". However, it had been set to "Deny Write".


SharePoint Anonymous Policy

There wasn't a good reason for this and switching it back to "None" fixed the problem.

Thursday, October 27, 2011

DataFormWebPart maximum field lengths

I used a DataFormWebPart to generate a custom edit form. Our client wanted to limit the maximum length of various fields on this form, but didn't want these to be applied site-wide.

The built-in SharePoint fields (FormField, TextField, NoteField, etc) don't have a "maxlength" property.

The solution was to modify the template for the various fields within the DataFormWebPart XSLT, to include RegularExpressionValidators.

Credit to Bart McDonough for this. His blog post relates to creating a new field type, but I tried it within the DataFormWebPart and it worked.

The nice thing about this is that the validation will take place on both the client and the server

Here is a sample from my DataFormWebPart XSLT file. This sets the maximum length of a text field to 80 characters.

Note that the curly braces in the regular expression have to be escaped. If you don't do this, the XSLT will break.



<SharePoint:TextField runat="server" id="ff1{$Pos}" ControlMode="Edit" FieldName="Title" __designer:bind="{ddwrt:DataBind('u',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}">

<Template>

<asp:TextBox id="TextField" runat="server"/>
<asp:RegularExpressionValidator
ControlToValidate="TextField"
ValidationExpression=".{{0,80}}"
Text="Value cannot exceed 80 characters."
runat="server" />

</Template>

</SharePoint:TextField>

Wednesday, October 26, 2011

SharePoint Check "page layout"

In code, I had a reference to an SPFile object and I wanted to check if the SPFile was a page layout file.

I used code (similar to the sample below) to do this. It checks to see if the file's parent list is the master page / page layout library.

Maybe you know a better way?


string url = " -- ABSOLUTE PATH TO YOUR FILE -- ";

using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPList masterPagelist = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
SPFile file = web.GetFile(url);

if (file.Item.ParentList.ID == masterPagelist.ID)
{
// The file is in the master page library.
}
}
}

Monday, October 24, 2011

SharePoint SummaryLinks "Edit Properties"

While logged-in as a user with Contributor permissions, I noticed that the toolbar for a Summary Links field was not visible on the Edit Form of a publishing page.

This is is shown in the picture below:

SummaryLinks has no

I suspect this is a SharePoint bug as the toolbar was visible when the page was in WYSIWYG edit mode.

Also, the issue did not occur when logged-in as a Site Collection administrator.

A work-around to this issue is to give the user the Manage Lists permission. However, this is really not something you should consider lightly as it gives editors an awful lot of power.

I'm going to speak to Microsoft to see what they say about it and then I'll post an update....

UPDATE


Microsoft has acknowledged that this is an "Offbug" (as they call it). In other words, it's a bug in their code.


This will be fixed in a future hotfix or service pack. However, they will not / cannot give a date for the fix.

Tuesday, September 13, 2011

SharePoint Inline HTML Editor

One of our clients had very specific requirements for the way in which their editors manage the main body content of a page. Here is some background:

With the previous version of SharePoint (MOSS 2007), page editors could edit page content using a Rich Text editor that sat above the content field. This is shown below:

MOSS RichText Editor

With SharePoint 2010, the Ribbon provides the editing functionality, as shown below:

Ribbon Editor

Even though our client is using SharePoint 2010, they wanted to use the MOSS-style page editor, rather than the Ribbon. This was for a subset of editors, on one specific site.

One way to solve this is to switch the User Interface (UI) version of the site from "4", to "3" e.g. switch from the SharePoint 2010 UI to the MOSS UI. This is done via the SPWeb.UIVersion property.

We didn't want to change the UI version so in the end, the solution was to:

1) Create a custom edit form.
2) Change the control used to edit the page content from SharePoint:FormField to SharePoint:InputFormTextBox

The InputFormTextBox has some properties that allow Rich Text, which are shown below:

<SharePoint:InputFormTextBox TextMode="MultiLine" Rows="6" RichText="true" RichTextMode="Compatible" runat="server" />