Monday, May 31, 2010

Customizing Sharepoint List's NewForm.aspx "OK" button click

Requirement: In a share point list, while adding a new item, a field value has to be validated depending on other field value in the list.

Solution:

A) Create a new aspx page for customizing newform.aspx.
  1. Open Share point Designer and Open the site.
  2. Go to the Share point list which you want to customize.(Lists => )
  3. Select "NewForm.aspx" file and copy it.
  4. Paste it under the same location where "NewForm.aspx" file exists and rename it to "Customized_NewForm.aspx".
B) Customizing newform.aspx.
  1. Open the "Customized_NewForm.aspx" file in designer in design mode.
  2. Select the "ListFormWebPart" and delete it.
  3. From Insert menu, click on "Sharepoint Controls" and then click on "Custom List Form".
  4. It will prompt you to select the List and content type. Select your list and Content type(Item). Also select "New Item Form" radio button for "Type of the form to create". Click on "OK" button.
  5. It will insert your default new item page. You can do your customizations on this page such as removing some fields, hiding some fields, renaming the field text displayed etc.
C) Adding JavaScript code for customizing "OK" button click event.
  1. Open the "Customized_NewForm.aspx" file in designer in Code/Split mode.
  2. Go to the Content Place holder, "PlaceHolderMain" and insert the following lines of Javascript code and save the file.
script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("StartUpCustomScript");
function StartUpCustomScript() {
ChangeOkButtonOnclickEvent("Input","SaveItem");
}
function ChangeOkButtonOnclickEvent(tagName, identifier) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName); /*find all Input type controls on page (ie. control with tag )*/
for (var i=0; i < tempstring =" tags[i].name;" func =" tags[i].attributes[" onclick =" null;" approvaltype =" document.getElementById(" selectedapptype =" ApprovalType.options[ApprovalType.selectedIndex].value;" smapprovers =" document.getElementById(" selectedapptype ="="" value ="="">

C) Setting Customized New Form as default new item page
  1. Open the list where "Customized_NewForm.aspx" file exists in Sharepoint designer and open the properties.(Right click the file and click on "Properties...").
  2. From "List Properties" window, select "Supporting Files" Tab.
  3. Select "Item" for "content type specific forms".
  4. For "New item form", click on Browse and select the customized file i.e. "Customized_NewForm.aspx" and click on "Ok".
  5. Now try to open the new form from list. Notice the url change to "Customized_NewForm.aspx" instead of "NewForm.aspx".
  6. Click on "Ok" and save the settings.

Wednesday, May 26, 2010

Unrecognized configuration section 'connectionStrings'

When I try to create a virtual directory for web service in IIS and try to browse the .asmx file, igot the following error
"Unrecognized configuration section 'connectionStrings'".
This is happening because by default the ASP.Net version selected is "1.1.4322" where as the code i have written is .Net version 2.0.
So, the solution is change the Asp.net version.
Go to the virtual directory, Properties. From ASP.NET tab, select "2.0.50727" for ASP.Net Version and click ok. Now it should work properly.

Tuesday, April 06, 2010

Error: “sgen.exe” exited with code 1

In VS 2005, it is building the solution in "Debug" mode correctly. But, when i try to build the solution in "Release" mode i got the error "'sgen.exe' exited with code 1".

Solution:

  1. Open the GAC (C:\Windows\Assembly), find the DLL corresponding to your project.
  2. Right click on it and select uninstall.
  3. Now, re-build your project and the problem must be gone.

Tuesday, March 02, 2010

Method to get the no.of working days between two dates excluding weekends

public static int CalculateBusinessDays(DateTime startDate, DateTime endDate)
{
//Check if Start Date is greater than end date, return -1
if (startDate > endDate)
return -1;

DateTime dateTemp;
int weekDays = 0;
int weekEnds = 0;
TimeSpan tsDiff = endDate.Subtract(startDate);
//Calculate number of weekend days between two dates
for (int i = 0; i < tsDiff.Days; i++)
{
dateTemp = startDate.AddDays(i + 1);//starting from next day of starting date
if (dateTemp.DayOfWeek == DayOfWeek.Saturday || dateTemp.DayOfWeek == DayOfWeek.Sunday)
weekEnds++;
}
weekDays = tsDiff.Days - weekEnds;
return weekDays;
}

Wednesday, February 17, 2010

Regular expression for Email in .net

const string expression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

Regex regex = new Regex(expression);

if (!regex.IsMatch(inputEmail))
{
return "Not a Valid Email";
}

Thursday, February 04, 2010

How to clear infopath repeating table data

public void ClearInfoPathTableData(string TABLE_PATH, List TableRowTitles)
{
int TempCount;

//Delete from last.
while (true)
{
TempCount = Root.Select(TABLE_PATH, NamespaceManager).Count;//Get the n.of rows to clear
if (TempCount == 1)
{
for (int count = 0; count < TableRowTitles.Count; count++)
{
XPathNavigator node = Root.SelectSingleNode(TABLE_PATH + "[1]/" + TableRowTitles[count], NamespaceManager);

if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
{
node.DeleteSelf();
}
node.SetValue("");
}
}
break;
}
Root.SelectSingleNode(TABLE_PATH + "[" + TempCount + "]", NamespaceManager).DeleteSelf();
TempCount = Root.Select(TABLE_PATH, NamespaceManager).Count;
}
}

Code to access querystring value from url in .net

if(e.InputParameters.ContainsKey("KeyName")) //Check if the url contains "KeyName" as querystring
{
string KeyValue = e.InputParameters["KeyName"];
}

Method to get Sharepoint Column Internal Names

#region Method to get the Internal Name for the Column in the List
public static string GetColumnInternalName(SPList List, string ActualColumnName)
{
string ColumnInternalName = string.Empty;
for (int fc = 0; fc < List.Fields.Count; fc++)
{
if (List.Fields[fc].Title == ActualColumnName)
{
//Get Internal Name for the displayed column name
ColumnInternalName = List.Fields[fc].InternalName;
return ColumnInternalName;
}
}
return ColumnInternalName;

}
#endregion

Getting the Field Values of Infopath Form stored in Sharepoint Form Library

//Get the Site
SPSite site= new SPSite(SITE_URL);
SPWeb Web = site.OpenWeb();
SPFile File = Web.GetFile(InfoPathDocUrl);

//Load the file into XML Document
XmlDocument Document = new XmlDocument();
Document.Load(File.OpenBinaryStream());

//Get the Root node and NamespaceManager of the Infopath form
XmlElement root = Document.DocumentElement;
XmlNamespaceManager NamespaceManager = InitNamespaceManager(Document);

//Get the Field values
string Field1Value= root.SelectSingleNode(Field1XPath, NamespaceManager).InnerText;
string Field2Value= root.SelectSingleNode(Field2XPath, NamespaceManager).InnerText;

Monday, February 01, 2010

How to update Infopath form fields Programatically?

public static void UpdateInfopathFormData(SPFile File, string NodeXPath, string UpdateValue,string NameSpace)
{
XmlDocument Document = new XmlDocument();
Document.Load(File.OpenBinaryStream()); // Get the FDRY document file into an XML Document

XPathNavigator root = Document.CreateNavigator();
XmlNamespaceManager nsm = new XmlNamespaceManager(Document.NameTable);
nsm.AddNamespace("my", NameSpace);//Get the NameSpace from infopath form(Open Infopath form in design mode. Click on datasource from design tasks.Click on any field. Go to properties. Go to Detalis Tab. Copy the "Namespace" value).

SetValueSpecial(root, NodeXPath, UpdateValue, nsm);
Byte[] data = Encoding.UTF8.GetBytes(Document.OuterXml);
File.SaveBinary(data);
}

public static void SetValueSpecial(XPathNavigator Root, string path, string value, XmlNamespaceManager NamespaceManager)
{

XPathNavigator node = Root.SelectSingleNode(path, NamespaceManager);

if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
{
node.DeleteSelf();
}
node.SetValue(value);
}

How to display hand icon on mouse over of Infopath Button?

1. Open the Infopath Form in Design mode.
2. Click on "Save as Source Files..." from File menu.
3. Go to the folder where Source files are stored.
4. Open the xsl file for which you want to display hand icon on buttons.(Note that it ill create an xsl file for each view of the infopath form).

5. Search for the button with "type="button" in search criteria
6. For that button style add "CURSOR: pointer;".
Ex:
input class="langFont" title="" style="BORDER-RIGHT: #cbd8eb 1pt solid; BORDER-TOP: #000000 1pt; FONT-WEIGHT: bold; FONT-SIZE: xx-small; BORDER-LEFT: #cbd8eb 1pt solid; WIDTH: 82px; COLOR: #ffffff; BORDER-BOTTOM: #000000 1pt; HEIGHT: 17px; BACKGROUND-COLOR: #00003c; CURSOR: pointer;" type="button" size="1" value="Close" xd:CtrlId="BtnClose" xd:xctname="Button"

7. Save the file.
8. Next publish the manifest file(manifest.xsf) to the Sharepoint library.
9. Now if you open the infopath file and mouse over the button, it will display hand icon instead of pointer.