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

Friday, May 27, 2011

SharePoint PublishingPageImage src

For an article page, SharePoint stores the value of the Publishing Image field in a particular format. Here is an example:

<img alt="" src="/SiteCollectionImages/home.jpg" style="BORDER: 1px solid; ">

I wanted to extract the value of the "src" attribute. Here is the code I used:

string fullValue = String.Format("{0}", file.Item[FieldNames.PUBLISHING_PAGE_IMAGE]);


string src = String.Empty;

if (String.IsNullOrEmpty(fullValue) == false)
{
string xml= String.Format("{0}</img>", fullValue);
string src = XElement.Parse(xml).Attribute("src").Value;
}

Wednesday, May 25, 2011

SharePoint "Object reference not set to an instance of an object" Page Save

I'd created a custom page layout, applied it to a page and then tried to edit the page. Everything was fine until I clicked on one of the save options for the page. At this point the site blew-up with the following stack trace:

NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.SharePoint.WebPartPages.WikiPageWebPartSaver.SaveWebPartsInRichText(SPWebPartManager wpmgr) +230

It took a bit of work but I tracked-down the cause. Essentially the Designer had given me some HTML mark-up which I'd pasted into the page layout. This mark-up contained an additional "form" element. When this was removed, the page-save worked correctly.

UPDATE

I don't remember the exact code, but it was something like the code below. The first form element is generated by SharePoint. The second form element was pasted-in by mistake.

<form id="aspnetForm" method="post" name="aspnetForm" action="default.aspx">

...

<form action="destination_url" method="get">
</form>


...

</form>

Friday, May 20, 2011

SharePoint "Item does not exist. It may have been deleted by another user"

I'd created an Event Receiver to handle the ItemDeleted event. I'd assigned this to a ContentType, which was then added to a Picture Library.

Click the image below to see what the code looked like. Initially, this seemed to work prefectly. (I'm using an image to show the code so that the text formatting is unmangled and read-able).



However, if I selected multiple pictures in the library and then clicked the "Delete" option from the library's menu, an unhandled exception was thrown which read "Item does not exist. It may have been deleted by another user".

I discovered the fix for this, which was to re-arrange the "try" blocks in my code. So instead of having the try-catch block surrounded by the try-finally block, I switched them so that the try-catch block is surrounded by the try-finally block.

I suspect the error was being generated because of a thread-timing issue, since (when the error occurred) the debugger wouldn't hit any breaks points in the code.

The re-worked code is shown below. I realise I could also reduce the code by using a try-catch-finally block, rather than 2 try blocks.

Thursday, May 19, 2011

SharePoint site "cannot be imported because its parent does not exist"

I ran a content deployment job from my farm to a farm on another machine. The job failed with the message:

The site /deleteme2/Lists/test1 cannot be imported because its parent does not exist.

Right now I cannot explain why this failed. "DeleteMe2" was a test site that I'd created and subsequently deleted. The site had defintely gone and my farm had no orphaned sites.

To work around the problem, I changed the deployment job so that it only exported a specific site, rather than the entire site collection.

This got me up-and-running and I'll continue to investigate.

Wednesday, May 11, 2011

SharePoint Always Prompts for Credentials

I had a SharePoint site running quite happily on my Dev VM. However, when I tried to connect to the site from my base machine, I was repeatedly prompted for credentials and could not connect.

This was surprising as the domain account I was logged-in as was the same for both the base machine and the Dev VM.

I tried a number of possible fixes, including:

1) Adding the site address to my Local Intranet zone
2) Disabling the loopback check
3) Checking that the firewall (running on the Dev VM) was not blocking access
4) Using a non-Internet Explorer browser

Nothing worked!

However, I thought I'd create a new SharePoint application and then try connecting to that. While doing this, the following error message was shown: "The trust relationship between this workstation and the primary domain failed."



The fix to this issue was to remove, then re-add the Dev VM to the domain. After I did this, I found the connection issue went away.