Programmatically update InfoPath form Content Type to open in browser

11 12 2009

If you are deploying an InfoPath form to your site as a feature then it is highly likely your form will be installed as a content type. By default however the form will try to open in the InfoPath client if there is one installed on the machine.

You could of course go into the list settings and change this so that new instances open in the browser but this is pain if you are trying to create an automated install/deploy process.

So how can I do this programmatically I hear you ask??

Well it turns out that the SPContentType class has a property called RequireClientRenderingOnNew. This is a boolean value which is by default set to ‘true’.

See the below code snippet on how to change this –

   1: SPContentType cType = web.ContentTypes["My ContentType"];

   2: cType.RequireClientRenderingOnNew = false;

   3: //Update the content type

   4: cType.Update();

The best place to perform this change would probably be in a ‘featurereceiver’.

Hope this helps :-)

UDH7UPSSP9QQ





Silverlight & Cross-Domain Policy

3 12 2009

If you are creating a Silverlight application that makes calls to a web service – either WCF or standard ASP.NET based then you will more than likely need an xml ‘policy’ file that allows cross domain access.

In my case I was calling one of the SharePoint inbuilt web services – lists.asmx from my silverlight application. When I tried to debug the application I received the following error –

An error occurred while trying to make a request to URI ‘http://localhost/sites/mysite/_vti_bin/lists.asmx’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place.

To fix this problem you need to create an xml file called ‘clientaccesspolicy.xml’ with the following content –

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="*" />
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

This xml file should be placed in the IIS virtual directory where your web service is located. In my case the SharePoint site collection that contained the web service was inside the IIS default website so I put the xml file in ‘C:\Inetpub\wwwroot’.

Once you have created this xml file make sure you do an IISRESET. Your silverlight application should now be calling the web service correctly and not throwing the above exception.

Hope this helps :-)





Getting started with Silverlight 3 and SharePoint

12 11 2009

Silverlight Logo Recently I have been focusing on creating some Silverlight charts using the ‘Silverlight Toolkit’ from Codeplex (see the link in the instructions below).

Once I had created my first chart I wondered how to get it to display in SharePoint. It seems there are a number of options for displaying a Silverlight application (xap) in a SharePoint web part.

I found lots of blog posts that described separate bits of configuring I needed to do to get Silverlight working happily in SharePoint. I’ve decided to create a getting started list of what you need to do to get Silverlight installed and how to display your xap file with the built in ‘Content Editor Web Part’. I have linked to other blog posts where necessary.

Configuration

1. Download and install the Silverlight 3 runtime from silverlight.net -

http://silverlight.net/getstarted/silverlight3/

2. Download and install the Silverlight 3 SDK and tools for Visual Studio from the silverlight site - 

http://silverlight.net/getstarted/

3. <Optional> Download and install the Silverlight Toolkit (If you want to use some of these cool and free Silverlight controls) -

http://silverlight.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30514

4. If you are running Windows Server 2008 you can skip this step as the MIME types should have been automatically added to IIS7 for you. For server 2003 users you will need to add the Silverlight MIME types to IIS6, follow the instructions here -

http://blogs.technet.com/jorke/archive/2007/09/11/silverlight-mime-types-in-iis6.aspx

5. This step involves configuring the web.config files for the SharePoint sites that you wish to run your Silverlight applications in. Follow the instructions on this blog post -

http://blogs.msdn.com/steve_fox/archive/2009/03/11/amending-the-web-config-file-to-support-silverlight-development-on-sharepoint.aspx

6. Ensure that the assembly System.Web.Silverlight is in the global assembly cache. If not then you can find it at the following location -

C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Server

7. Configuration complete! – now read on to the deployment section

Deployment

To get your silverlight xap displaying on a SharePoint page follow the steps below. In terms of where to store your .xap file there are a number of places for you to choose. Some people recommend storing it in a folder called ‘ClientBin’ in the IIS virtual directory of the SharePoint web application but I was not able to get this working. I opted for the simplest method which was to store the file in a document library.

1. Upload your Silverlight xap file to a document library

2. Switch to edit mode for your SharePoint page and add a Content Editor Web Part

3. Insert the following HTML code - 

<!–<div width=”600px” height=”100px” id=”silverlightControlHost”>
<object data=”data:application/x-silverlight”, type=”application/x-silverlight-2″ width=”450″ height=”450″>
<param name=”source” value=”
http://yoursite/sites/charting/XAPs/SimpleSilverlightChart.xap”/>
<param name=”onerror” value=”onSilverlightError” />
<param name=”background” value=”white” />
<a href=”
http://go.microsoft.com/fwlink/?LinkID=108182″ style=”text-decoration: none;”>
<img src=”
http://go.microsoft.com/fwlink?LinkID=108101″ alt=”Get Microsoft Silverlight” style=”border-style: none”/>
</a>
</object>
<iframe style=’visibility:hidden;height:0;width:0;border:0px’></iframe>
</div> –>

Remember to remove the comments from the above code and replace the param value=”” with the url to your document library and xap file.

4. Click apply and save the changes to your content editor web part

5. If all went well you should be seeing your Silverlight application displaying correctly!

Note: You may need to play around with the width and height of both the <div> and <object> tags to size them correctly for your Silverlight application.

Good Luck! :-)





Microsoft Chart Controls for .NET – Error executing child request for ChartImg.axd

3 11 2009

I ran into this error today when attempting to show a web part on a SharePoint site that utilises the .NET 3.5 Chart Controls from MS (See link at the bottom of the post).

When the page opened it bombed out with the above error, some research found the following question on stackoverflow –

http://stackoverflow.com/questions/302820/net-3-5-chart-controls-exception-error-executing-child-request-for-chartimg-axd

To fix this error you need to add the following entry into the <HttpHandlers> section of the web.config file:

<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />

When I refreshed the page, I received another error – this time it was:

Invalid temp directory in chart handler configuration

If you too get this error then you need to make sure you have the following line in the <appSettings> section of the web.config:

<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />

Also make sure that the above directory ‘TempImageFiles’ exists on the C: drive or you can change this to any other directory that you like.

Hope this helps! :-)

Download link for MS Chart Controls:

http://www.microsoft.com/downloads/details.aspx?FamilyId=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=en





SPListitem.File.Versions – System.Argument.Exception

28 10 2009

I was writing some code yesterday to loop through the list items in a SharePoint list and then loop through each version of the SPListItem’s underlying file.

Here is the code I was using -

using (SPSite site = new SPSite("http://theserver/"))
           {
               using (SPWeb web = site.OpenWeb())
               {
                   SPList list = web.Lists["The List"];
                   SPView view = list.Views[new Guid("D9E7BDDC-4C77-4386-BA0E-A786D58EE199")];

                   SPListItemCollection itemCol = list.GetItems(view);

                   foreach (SPListItem item in itemCol)
                   {
                       foreach (SPFileVersion version in item.File.Versions)
                       {
                           string url = version.Url;
                       }

                   }
               }
           }

When the code hit the ‘item.File.Versions’ collection it threw a System.Argument.Exception. This really stumped me and I couldn’t figure out at all while the collection was throwing this exception.

After a lot of researching I found this blog post –

http://www.mtelligent.com/journal/2007/10/17/the-insanity-of-getting-versions-of-a-multilinetext-box-set-.html

In this post, David talks about getting the same exception when he is trying to access the list item’s file versions. If you scroll down and look at the comments you you will see that this only seems to be a problem if you are getting the SPListItem as the result of an SPQuery (in my case the spquery is actually an SPView).

This is most likely because getting the item using an SPQuery returns the list item with a minimum amount of data, therefore the versions are not accessible.

The solution here is to create another instance of the SPListItem by using the GetItemById() method which will return all the properties for the list item. This way the SPListItem.File.Versions collection will be accessible and we can iterate through it.

See the correct code –

using (SPSite site = new SPSite("http://theserver/"))
           {
               using (SPWeb web = site.OpenWeb())
               {
                   SPList list = web.Lists["The List"];
                   SPView view = list.Views[new Guid("D9E7BDDC-4C77-4386-BA0E-A786D58EE199")];

                   SPListItemCollection itemCol = list.GetItems(view);

                   foreach (SPListItem item in itemCol)
                   {

//Get a reference to the item again by the ID

SPListItem theItem = list.GetItemById(item.ID);

//The versions collection should now be populated (if versions are available) and no exception should be thrown

                       foreach (SPFileVersion version in theItem.File.Versions)
                       {
                           string url = version.Url;
                       }

                   }
               }
           }

Hope this helps someone! :-)





Copying files to the 12 Hive with VSEWSS Extensions 1.3

26 10 2009

This had me stumped for a while earlier, I had created a Web Part project and wanted to include an image to be deployed to the TEMPLATE\IMAGES directory of the 12 hive.

Originally I added a module to the project but this would then copy the image to a folder in the actual site itself which I didn’t want.

The solution is to use a ‘Template’ –

Right click on the project in Visual Studio and select Add > New Item

Add new template to the project

Select a ‘Template’ and give it a name, this will add a folder in your solution called ‘Templates’ with a text file inside that has the name you gave above.

You can then add the 12 hive TEMPLATES folder structure under here and any files will be copied to the 12 hive when the wsp solution package is installed.

Templates folder within solution

Hope this helps!





SPBuiltInFieldId – Very useful

22 10 2009

If you ever need to get the GUID of a built in SharePoint field such as ‘IssueStatus’ (from the standard ‘Issue Tracking’ list) for example then you can use the SPBuiltInFieldId class.

This class contains a number of GUID objects for each type of built in SharePoint field.

To use it simply use the following code:

Guid issueStatus = SPBuiltInFieldId.IssueStatus;

I found this very useful, hope it helps some of you!





VseWSS 1.3 CTP March 09 – Still Buggy

15 10 2009
I’ve been developing a web part today and thought I would try the VseWSS extensions to see if they would get the job done quicker, I normally just create the Visual Studio solution myself and use WSPBuilder (http://www.codeplex.com/wspbuilder) to deploy it.

After I had built my web part I clicked to debug and was presented with the following error:

clip_image002_2

To get past this error I used the following two blog posts:

http://blogs.msdn.com/steve_fox/archive/2009/03/18/vsewss-1-3-post-install-configuration.aspx

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/02/05/vsewss-1-3-ctp-feedback-and-feature-requests.aspx

It seems that the error is because the account running the application pool for VseWSS web application in IIS does not have the correct permissions. I was on a domain controller so simply gave the account domain admin permissions. This however did not fix the error and I ended up giving the account full control to the folder itself!

When I tried to deploy again I did not get the above error but now a different one (See screenshot below)! I cannot find any more details on this error and the folders specified in the error message do not even exist.  I think im going to go back to using WSPBuilder from now on as it seems the VseWSS extensions are still quite buggy.

Untitled

If anyone has any more details on the above error and how to fix it then please let me know :-)





Get URL Value of SharePoint Hyperlink Field

12 10 2009

I needed to get the value of a SharePoint hyperlink field in a custom list the other day so I simply tried to access the value of the field directly in the object model:

SPListItem item = list.GetItemById(1);

string URL = item[“URL”].ToString();

The only problem with this is whilst it gets the value, you end up with two URL’s separated by a comma. This is because one is the actual display text for the field and the other is the value (the actual URL itself).

In order to get just the URL value, you need to use the following object model code:

SPFieldUrlValue value = new SPFieldUrlValue(item[“URL”].ToString());

string URL = value.Url;

You can also use value.Description to get the actual display text for the field.

Simples! Hope this helps :-)





Obtaining workflow status in code

14 09 2009

I recently wrote a console application which looped through the workflows running on my SharePoint site and showed the status of each one. When I refer to status I mean the same status that is displayed under the workflow column e.g. In Progress, Error occurred etc

To get the workflow status in code you need to use the enumeration ‘SPWorkflowStatus’, which will return one of the following numeric values:

public enum SPWorkflowStatus
{
NotStarted = 0,
FailedOnStart = 1,
InProgress = 2,
ErrorOccurred = 3,
StoppedByUser = 4,
Completed = 5,
Max = 15,
}

Hope this helps!