Powered By Blogger

Monday, November 28, 2011

How to create custom Application Page (aspx page) for SharePoint 2010 using VS 2010


1. In Visual Studio 2010 IDE create new “Empty SharePoint Project” by going into File > New > Projects > SharePoint > 2010. Make sure to select .Net Framework 3.5 in version selector drop down list.

2.







2. Select farm solution as trust level for debugging this application.











3.Right click on project in Solution Explorer and add new item and add “Application Page”. This will add an aspx page in a Mapped “Layouts” (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS) folder. This is because when you will deploy your application page to SharePoint it will host this application physically into Layouts directory.










4. Write some code in .cs file of aspx page as I have written a two number add program

Open aspx page of application page and refer page directive at the top “<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyApplicationPage.aspx.cs" Inherits="SharePoint2010ApplicationPage.Layouts.SharePoint2010ApplicationPage.MyApplicationPage" DynamicMasterPageFile="~masterurl/default.master" %>It do reference ~masterurl/default.master. This application page can be accessed by using any site collection url in SharePoint farm. Using url like ( http://yoursite/_layouts/ApplicationPage.aspx).
















5. Now all development done, we should deploy this application page to SharePoint Server. To deploy on development server you just have to right click on project in solution explorer and select deploy, It will deploy the application on development machine Layout folder.

To deploy the same on production environment we need to create a solution package (wsp package ) , Creating wsp package in Visual Studio 2010 get very easy as you package by right clicking on project file and selecting package it will create a wsp file in bin folder.

Friday, November 25, 2011

Add / Insert records to sql server database using InfoPath programmatically

Adding values to sql database using InfoPath form is possible using code, same as we do in asp.net. This technique will work for the both InfoPath filler and browser form.

1. Design a form and add required fiend to main data source. Drag them to design surface. Add a button control.
















2. Right click on Submit button and click "Edit Form Code" button. It will open VSTA code editor window. Add your code as below it has been added for the above form.

using Microsoft.Office.InfoPath;

using System;

using System.Xml;

using System.Xml.XPath;

using System.Data.SqlClient;

using System.Data;

namespace AddItemToSql

{

public partial class FormCode

{

public void InternalStartup()

{

((ButtonEvent)EventManager.ControlEvents["btnSubmit"]).Clicked += new ClickedEventHandler(btnSubmit_Clicked);

}

public void btnSubmit_Clicked(object sender, ClickedEventArgs e)

{

//Navigator object to navigate through Main Datasource

XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();

string strConString = "";

string strLocation = string.Empty;

string strGrade = string.Empty;

string strItem = string.Empty;

string strDepartment = string.Empty;

string strEmployee = string.Empty;

//Taking value from infopath's main datasource fields.

strLocation = xNavigator.SelectSingleNode("/my:myFields/my:Location", NamespaceManager).Value;

strGrade = xNavigator.SelectSingleNode("/my:myFields/my:Grade", NamespaceManager).Value;

strItem = xNavigator.SelectSingleNode("/my:myFields/my:Group/my:Item", NamespaceManager).Value;

strDepartment = xNavigator.SelectSingleNode("/my:myFields/my:Group/my:Dept", NamespaceManager).Value;

strEmployee = xNavigator.SelectSingleNode("/my:myFields/my:Group/my:Emp", NamespaceManager).Value;

SqlConnection sqlCon = new SqlConnection(strConString);

SqlCommand sqlCmd = new SqlCommand();

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.CommandText = "";

sqlCmd.Parameters.AddWithValue("@Location", strLocation);

sqlCmd.Parameters.AddWithValue("@Grade", strLocation);

sqlCmd.Parameters.AddWithValue("@Item", strLocation);

sqlCmd.Parameters.AddWithValue("@Dept", strLocation);

sqlCmd.Parameters.AddWithValue("@EmpId", strLocation);

sqlCmd.Connection = sqlCon;

try

{

sqlCon.Open();

int result = sqlCmd.ExecuteNonQuery();

if (result &gt; 0)

{

xNavigator.SelectSingleNode("/my:myFields/my:Message", NamespaceManager).SetValue("Item Added !!"); ;

}

else

{

xNavigator.SelectSingleNode("/my:myFields/my:Message", NamespaceManager).SetValue("Fail !!"); ;

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

sqlCon.Close();

}


3. To test preview and submit by adding values in text boxes.
























XPathNavigator class used to read and write values in InfoPath form field.


XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();


To read value from InfoPath form field

xNavigator.SelectSingleNode("/my:myFields/my:Group/my:Emp", NamespaceManager).Value;

To write value in InfoPath form field

xNavigator.SelectSingleNode("/my:myFields/my:Message", NamespaceManager).SetValue("Item Added !!");