Thursday, 7 August 2008

XmlFormView used in a Sharepoint Environment Error

When following the tutorial on deploying an infopath form in asp.net web page into sharepoint you might get the strangest of errors saying

"The type 'Microsoft.Office.InfoPath.XmlForm' is defined in an assembly that is not referenced."

If you get this ensure that the web.config file for the SharePoint contains the Infopath Assemblies

-----web.config ----








---web.config----

This is the url that walks thru the hosting of the infopath forms in a custom page. Please be aware that the tutorial only caters for situations where you are hosting the .net page in the root site collection.

http://msdn.microsoft.com/en-us/library/aa701078.aspx#infopathxmlformviewcontrolcustomwebpage_deployingnonroot

Using query string with InfoPath forms

Sometimes it is necessary to accept query string for browser enabled info path forms. This can be done by hosting the Infopath form in a asp.net page and hosting that page within Sharepoint using the XmlFormView class.

This technical guide explains the process

http://msdn.microsoft.com/en-us/aa701078.aspx

The code behind for the asp.net page will contain a method like the one shown below..

protected void XmlFormView1_Initialize(object sender, InitializeEventArgs e)
{
string positionAppliedFor = Request.QueryString["Position"];
string jobID = Request.QueryString["JobID"];
string appURL = Request.QueryString["AppURL"];

if (!string.IsNullOrEmpty(positionAppliedFor) && (!string.IsNullOrEmpty(jobID)))
{
// Create an XPathNavigator positioned at the root of
// the form's main data source.
XPathNavigator xNavMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
// Create an XmlNamespaceManager and add the "my" namespace
// alias from the form's main data source.
XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());
xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-07-08T12:49:29");
// Create an XPathNavigator positioned on the form's field2.
XPathNavigator fTextBoxPosition = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtPositionAppliedFor", xNameSpace);
// Set the form's job Position Value to the value passed in the query strings
fTextBoxPosition.SetValue(positionAppliedFor);

XPathNavigator fTextBoxJobID = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtJobID", xNameSpace);
fTextBoxJobID.SetValue(jobID);

if (!string.IsNullOrEmpty(appURL))
{
XPathNavigator fTextBoxAppURL = xNavMain.SelectSingleNode(
"/my:myFields/my:gpRecommendation/my:txtAppURL", xNameSpace);
fTextBoxAppURL.SetValue(appURL);
}
}
}

Installing SharePoint on a simple farm

Very comprehensive guide on installing SharePoint on a simple farm

http://technet.microsoft.com/en-us/library/cc262243.aspx

Validating Currency fields regular expression

Expression: ^\d{0,2}($|\.\d{0,2}$)
Example of valid values:
20.09
1.99
1.2
3
.5

Example of invalid values:
120.09
1.991
x

Wednesday, 14 May 2008

Validating Custom Web Part Properties - the proper way!

Adding validation to custom web part properties (the proper way!) Thanks Ken Josling for this info!

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.IO;
using System.Resources;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace YourNamespace
{

public class YourWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private bool invalidConfiguration;
private string exceptionMessage;
private ResourceManager resourceManager;

public YourWebPart()
{
invalidConfiguration = false;
exceptionMessage = string.Empty;
resourceManager =
new ResourceManager("YourNamespace.Resource", GetType().Assembly);
}

private void invalidateConfiguration(string message)
{
invalidConfiguration = true;
exceptionMessage = message;
}

protected void AddInitializeControl()
{
bool hasRights = ((ISecurableObject)SPContext.Current.Web).DoesUserHavePermissions((SPBasePermissions)Microsoft.SharePoint.SPPermissionGroup.WebDesigner);

if (!hasRights)
return;

string editLink = Microsoft.SharePoint.WebPartPages.ToolPane.GetShowExtensibleToolPaneEvent(string.Format(@"'{0}'", this.UniqueID));
this.Controls.Add(new LiteralControl(
string.Format(exceptionMessage + resourceManager.GetString("WebPartHelpMessage"), editLink.Replace(@"ExtensibleView", @"Edit"))
));

}

protected override void CreateChildControls()
{
//Add your validation logic here
if(x)
{
invalidateConfiguration(resourceManager.GetString("SomeErrorMessage"));
}
if (y)
{
invalidateConfiguration(resourceManager.GetString("SomeOtherErrorMessage"));
}

if (invalidConfiguration)
{
AddInitializeControl();
}
else
{
//Render Web Part Here
}
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}

Monday, 12 May 2008

Trying to use an SPWeb object that has been closed or disposed and is no longer valid

Trying to use an SPWeb object that has been closed or disposed and is no longer valid

When you get the error message above it is most likely that you have got a reference to the SPWeb object by using the SPContext as shown below

SPWeb web = SPContext.Current.Web;

There is no problem with using this method but you need to make sure that you do not close or dispose this SPweb object becase the SPContext.Current.Web is used by the Page and all the controls on the page.

So you should not use this web object in Using statements also.

Wednesday, 30 April 2008

Working with SharePoint Groups

This is a nice article that talks about working with SharePoint groups programmatically.

The only problem I had was I had to replace the lines
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;

with SPRoleDefinition and added this role definition directly to the SPweb as shown below

SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);
def = web.RoleDefinitions["Full Control"];
roleAssignment.RoleDefinitionBindings.Add(def);
web.RoleAssignments.Add(roleAssignment);
web.Update();

http://www.emptycache.com/blog/2008/04/18/working-with-users-and-groups-in-sharepoint-2007/