Saturday, March 19, 2011

C# code to insert items in SharePoint List

Create a web part in visual studio and deploy it into share point application.

Below is the code to insert item into a SharePoint list.

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace customlist

{

//Below Guid can be genrated from from Tools menu of Visual Studio and then choose create GUID

[Guid("EABAFAC0-0288-4292-9007-B8AAB8BC45DA")]

public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart

{

TextBox oTextTitle;

TextBox oTextName;

TextBox oTextDesignation;

Label oLabelMessage;

Button oButtonSubmit;

//Constructor

public WebPart1()

{

}

//Adding controls in below code block

protected override void CreateChildControls()

{

base.CreateChildControls();

oTextTitle = new TextBox();

this.Controls.Add(oTextTitle);

oTextName = new TextBox();

this.Controls.Add(oTextName);

oTextDesignation = new TextBox();

this.Controls.Add(oTextDesignation);

oLabelMessage = new Label();

this.Controls.Add(oLabelMessage);

oButtonSubmit = new Button();

oButtonSubmit.Text = "Submit";

oButtonSubmit.CssClass = "ms-ButtonHeightWidth";

this.Controls.Add(oButtonSubmit);

oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);

}

//Below Code block will execute when user will click on Submit Button

void oButtonSubmit_Click(object sender, EventArgs e)

{

if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")

{

oLabelMessage.Text = "You must specify a value for this required field";

}

else

{

SPSite mySite = SPContext.Current.Site;

SPWeb myWeb = SPContext.Current.Web;

SPList myList = myWeb.Lists["your list name"];

SPListItem myListItem = myList.Items.Add();

myListItem["Field1/Column1"] = oTextTitle.Text.ToString();

myListItem["Field2/Column2"] = oTextName.Text.ToString();

myListItem["Field2/Column2"] = oTextDesignation.Text.ToString();

myWeb.AllowUnsafeUpdates = true;

myListItem.Update();

myWeb.AllowUnsafeUpdates = false;

oLabelMessage.Text = "Recorded inserted";

oTextTitle.Text = "";

oTextName.Text = "";

oTextDesignation.Text = "";

oTextTitle.Focus();

}

}

For more about sharepoint please visit : http://sharepointcafe.net

C# code to insert items in SharePoint List

Create a webpart in visual studio and deploy it into sharepoint application.

Below is the code to insert item into a SharePoint list.

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace customlist

{

//Below Guid can be genrated from from Tools menu of Visual Studio and then choose create GUID

[Guid("EABAFAC0-0288-4292-9007-B8AAB8BC45DA")]

public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart

{

TextBox oTextTitle;

TextBox oTextName;

TextBox oTextDesignation;

Label oLabelMessage;

Button oButtonSubmit;

//Constructor

public WebPart1()

{

}

//Adding controls in below code block

protected override void CreateChildControls()

{

base.CreateChildControls();

oTextTitle = new TextBox();

this.Controls.Add(oTextTitle);

oTextName = new TextBox();

this.Controls.Add(oTextName);

oTextDesignation = new TextBox();

this.Controls.Add(oTextDesignation);

oLabelMessage = new Label();

this.Controls.Add(oLabelMessage);

oButtonSubmit = new Button();

oButtonSubmit.Text = "Submit";

oButtonSubmit.CssClass = "ms-ButtonHeightWidth";

this.Controls.Add(oButtonSubmit);

oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);

}

//Below Code block will execute when user will click on Submit Button

void oButtonSubmit_Click(object sender, EventArgs e)

{

if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")

{

oLabelMessage.Text = "You must specify a value for this required field";

}

else

{

SPSite mySite = SPContext.Current.Site;

SPWeb myWeb = SPContext.Current.Web;

SPList myList = myWeb.Lists["your list name"];

SPListItem myListItem = myList.Items.Add();

myListItem["Field1/Column1"] = oTextTitle.Text.ToString();

myListItem["Field2/Column2"] = oTextName.Text.ToString();

myListItem["Field2/Column2"] = oTextDesignation.Text.ToString();

myWeb.AllowUnsafeUpdates = true;

myListItem.Update();

myWeb.AllowUnsafeUpdates = false;

oLabelMessage.Text = "Recorded inserted";

oTextTitle.Text = "";

oTextName.Text = "";

oTextDesignation.Text = "";

oTextTitle.Focus();

}

}

Sunday, February 27, 2011

Creating and deploying Application page to your SharePoint application

What are application page?
Actually there are two types of page available in SharePoint.
1. Site Pages: Those pages which supports user customization by some designing tools e.g. SharePoint designer.
2.Application Pages: The pages which does not support user customization. You can find application pages in \Layout directory. You can create your own application page and you can deploy it to your sharepoint application.

Open visual studio and create a simple web application.

Paste the below code in your .aspx page.

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, 

Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"
Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase"%>

<%@ Import Namespace="Microsoft.SharePoint" %>



"Main" runat="server"
contentplaceholderid="PlaceHolderMain" >
Site Title: "lblSiteTitle" runat="server" />


Site ID: "lblSiteID" runat="server" />


"PageTitle" runat="server"
contentplaceholderid="PlaceHolderPageTitle" >
Hello World


"PageTitleInTitleArea" runat="server"
contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
The Quintessential 'Hello World' of Application Page


Deployment

Note that although you can deploy your application pages directly inside the \LAYOUTS directory, doing so can create file name conflicts between your application pages and those application pages that are created by Microsoft and other companies. It is a best practice to deploy your application pages inside a company-specific or project-specific directory that is nested with the \LAYOUTS directory. For example, you can deploy application pages within a company-specific directory located at the path \LAYOUTS\your-project-folder-name

Now you can see your application page in your SharePoint application.

Lots more on Application page....coming soon......




Monday, February 7, 2011

Creating a Simple web part

Here are the steps to create a simple web part in Visual Studio and deploy it into your sharepoint application:

Open Visual Studio and select a web part from project template.

Add Refrence:

Microsoft.SharePoint;

Now write below code:

protected override void CreateChildControls()
{
Lable lbl = new Lable();
lbl.Text="Hello To SharePoint Coding Blogs";
this.controls.add(lbl);
}

Build the Application.
Go to your project folder
And under Bin folder you will find .dll file.
Drag this .dll file into C:\windows\assembly

Now go to C:\inetpud\wss\Virtual Directory\your project name

Open web.config file

And under safe control tag put this code:

namespace= namespace name, Assembly=your assembly name, culture=nuetral
version=12.0.0.0, PublicKeyToken=7888etr77


Go to your Sharepoint application.
Site Actions->Siet settings

Under gallery section click on web part.
Click new.
Then select your class file and populate it.
Link
After that go to your site page and click edit page under site actions.

Click on add a webpart.
Select your webpart from web part gallery.

That's done.


For more please visit : www.sharepointcafe.net