Wednesday, 9 October 2013

Choice Fields in SharePoint 2010 Programmatically Using VS 2010

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010.

Q: What is a list?
In simple terms "When you create a custom list, a new empty list is created with just two columns - Title and Attachments. The list contains a single default view. Once you create the list, you can add more columns, views, and so on".

I think we are now good to go and implement this wonderful concept.

Step 1: Create a custom list and modify the view.

Step 2: Open SharePoint 2010 Central Administration and navigate to a specific site.

Step 3: Open up Visual Studio 2010 and create an "Empty SharePoint project":

choicefield1.gif
Step 4: Select "Deploy as a farm solution" and click on the "Finish" button. Now an empty project will be created:

choicefield2.gif
Step 5: Add a new visual webpart for that project.

choicefield3.gif
Step 6: The complete code of visualwebpart1usercontrol.ascx looks like this:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChoiceWebPartUserControl.ascx.cs"
    Inherits="ChoiceSharePoint.ChoiceWebPart.ChoiceWebPartUserControl" %>
<div style="text-align: center;">
    <table>
        <tr>
            <td colspan="2">
                <asp:Label ID="Label1" runat="server" Text="List Choice via Object Modelling - SharePoint 2010"
                    Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Please Enter First Column Name" Font-Size="Large"
                    Font-Names="Verdana" Font-Italic="true"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Please Enter Second Column Name" Font-Size="Large"
                    Font-Names="Verdana" Font-Italic="true"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Please Enter Third Choice Column Name"
                    Font-Size="Large" Font-Names="Verdana" Font-Italic="true"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <br />
                <br />
                <fieldset>
                    <legend style="font-family: Verdana; font-weight: bold; font-style: italic">Enter ChoiceList
                        Items</legend>
                    <br />
                    <div>
                        <center>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="Label6" runat="server" Text="Please Enter First List Item Name " Font-Size="Large"
                                            Font-Names="Verdana" Font-Italic="true"></asp:Label>
                                    </td>
                                    <td style="text-align: right">
                                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="Label7" runat="server" Text="Please Enter Second List Item Name "
                                            Font-Size="Large" Font-Names="Verdana" Font-Italic="true"></asp:Label>
                                    </td>
                                    <td style="text-align: right">
                                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                            <br />
                            <br />
                        </center>
                    </div>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="Button2" runat="server" Text="Create List Fields" Font-Names="Verdana"
                    Width="166px" BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
            </td>
        </tr>
    </table>
</div>

Step 7: The complete code of visualwebpart1usercontrol.ascx.cs looks like this:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;


namespace ChoiceWebPartUserControl
{
    public partial class ChoiceWebPartUserControl : UserControl 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty
                (TextBox2.Text) ||string.IsNullOrEmpty(TextBox3.Text) || string.IsNullOrEmpty
                (TextBox4.Text) ||string.IsNullOrEmpty(TextBox5.Text))
            {
                Label5.Text = "Please Enter Some Values";
                Label5.ForeColor = System.Drawing.Color.Red;
            }
            else
             {
                SPSite site = SPContext.Current.Site;
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Person"];
                SPListItem item = list.Items.Add();
                item.Fields.Add(TextBox1.Text, SPFieldType.Text, true);
                item.Fields.Add(TextBox2.Text, SPFieldType.Text, true);
                SPFieldChoice fieldChoice = (SPFieldChoice)list.Fields.GetFieldByInternalName
                    (item.Fields.Add(TextBox3.Text, SPFieldType.Choice, true));
                string[] types = { TextBox4.Text, TextBox5.Text };
                fieldChoice.Choices.AddRange(types);
                fieldChoice.Update();
                SPView view = list.DefaultView;
                view.ViewFields.Add(TextBox1.Text);
                view.ViewFields.Add(TextBox2.Text);
                view.ViewFields.Add(TextBox3.Text);
                view.Update();
                Label5.Text = "Fields Created Successfully";
                Label5.ForeColor = System.Drawing.Color.Green;
                TextBox1.Text = string.Empty;
                TextBox2.Text = string.Empty;
                TextBox3.Text = string.Empty;
                TextBox4.Text = string.Empty;
                TextBox5.Text = string.Empty;
            }
        }
    }
}


Step 8: Deploy the solution file and add the created webpart to a SharePoint site:

Step 9: The output of the application looks like this:



Step 10: Choice fields entering output of the application looks like this:




Step 11: Data entering in list output of the application looks like this:



Step 12: Data entered in list output of the application looks like this:

How to Create/Update/Delete SharePoint 2010 list items using Client Object Model

I have created one list named as Test in the SharePoint 2010 site with one column "Title". I will be creating Console Application using Visual Studio 2010 to do create, update and delete methods.


Create List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as CreateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;namespace CreateListItem
{
    class Program    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:111/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("Test");
            ListItemCreationInformation listCreationInformation = newListItemCreationInformation();
            ListItem oListItem = oList.AddItem(listCreationInformation);
            oListItem["Title"] = "Item1";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.

Update List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as UpdateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace
 UpdateListItem
{
    class Program    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:111/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("Test");
            ListItem oListItem = oList.GetItemById(5);
            oListItem["Title"] = "My Updated Item.";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a item is updated.
Delete List Item:

  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as DeleteListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace
 UpdateListItem
{
    class Program    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:111/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("Test");
            ListItem oListItem = oList.GetItemById(5);         
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}

  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.

How to create Sample SharePoint 2010 Visual web part using Visual Studio 2010

What is a Visual Web Part?

With SharePoint 2007 and Windows SharePoint Services 2007 or previous versions, to develop custom web parts for SharePoint sites the developer has to write the entire code from scratch. Also, the developer has to keep in mind all the design issues, layouts and rendering of the web part. This was so cumbersome that developers had to be careful while writing code. A single mistake while writing the design code could mess up the whole layout.

But now using Visual Studio 2010, we can develop a custom web part for SharePoint 2010 site using the Visual designer. Visual Studio 2010 provides a new project template for Visual Web Part. This project provides us the facility to develop a custom web part by dragging and dropping the different web controls onto the designer surface.

Now we will see how to develop a custom web part using this new feature provided Visual Studio 2010. Following are the steps for creating a Visual Web Part project using Visual Studio 2010.

Steps Involved:


Creating Empty SharePoint Project:

Open Visual Studio 2010.
  • Go to File => New => Project. 
  • Select 2010 from the installed templates SharePoint and choose Empty SharePoint Project. 
  • Name it as HelloWorld.
  • Click Add. 
  • Enter the SharePoint server farm URL. 
  • Select 'Deploy it as a Farm Solution". 
  • Click Finish.
Creating Visual web part:

  • Right click the Solution Explorer and select Add a new item. 
  • Select Visual Web Part from the installed templates SharePoint and name it as HelloWorld.

1.gif
  • Once you create Visual Web Part automatically all the files will be generated as shown in the below figure that is required to deploy your web part directly into the SharePoint 2010.
  • In the solution explorer you could see VisualWebPart1UserControl.ascx.
  • Open the .ascx file and add one asp label control.
  • In this web part I am just going to display a label with the text as "Hello World!!!!".
  • See the below code.
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %><%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register Tagprefix="asp" Namespace="System.Web.UI"Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %><%@ Import Namespace="Microsoft.SharePoint" %><%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Control Language="C#" AutoEventWireup="true"CodeBehind="VisualWebPart1UserControl.ascx.cs"Inherits="HelloWorld.VisualWebPart1.VisualWebPart1UserControl" %>
<asp:Label ID="lblHelloWorld" runat="server" Font-Bold="True"
    ForeColor="#0066FF" Text="Hello World!!!!"></asp:Label>

  • Build the solution. 
  • Deploy the solution.
Testing:

  • Go to the SharePoint site  http://servername:2010.
  • Go to Site Actions=>Edit Page.
  • In the top ribbon Editing Tools => Insert => Web Part, click on the Web Part. 
2.gif
  • Go to Categories => Custom, you could see VisualWebPart1 which we have developed using Visual Studio 2010.

    3.gif

  • Select VisualWebPart1 and click on Add.
  • Now you will able to see the label with the text "Hello World !!!!" as shown below.



Summary:

    In this article my intension is to give some information about how to create a simple Visual Web part (a new template that is available in visual studio) and deploy directly into the SharePoint 2010.
References:

Tuesday, 8 October 2013

Deleting Web Application in SharePoint 2010

1. Go to SharePoint Central Admin



2. In SharePoint Central Admin Home page go to Application Management Section



3. Click on Manage web application

4. In web Application Management Page select your web application



5. Two delete options available



Delete Web Application

This option allows you to delete the associated content db and IIS site



Remove SharePoint from IIS site

This option allows you to delete the IIS website associated to this web application.One thing to note here it will not delete the associated Content DB




6. Choose Delete Web Application
7. Set Delete Content Database yes
8. Set Delete IIS Site Yes.
9. Click OK

Service Application Framework in SharePoint 2010

What is Service Application Framework?

  • Replacement for the Shared Services Provider in MOSS 2007.
  • API provided by backend application servers and consumed by front-end application servers.
  • Used for developing middle-tier applications that are hosted in SharePoint and provide data and resources to other SharePoint features.
  • Enables services to be shared between computers on a server farm.
  • Load balance and manage services.
  • Out of the box - 20 built in services.
  • Certain objects automatically backed up and restored.
  • Ideal for deploying, managing and discovering WCF service clients and endpoints.
  • Windows Powershell support.
  • Timer job infrastructure available at Service-scope.
  • Use of SharePoint configuration store and support for storing data in SharePoint managed custom database is available.

Load Balancing
  • Use Round robbing load balancing.
  • Service application proxy method invocations must be routed thru the front end web server to an appropriate app server by using a load balancing tool.
  • Calls between frontend and app servers to require separate external load balancer than the load balancing for front-end web servers.
  • SPRoundRobinServiceLoadBalancer can be enhanced or replaced by third party

Management and Administration
  • Services plug their management UI into SharePoint Service Management page.
  • Common admin tools such as upgrade, backup, restore and account management.
  • Common UI to manage, start, stop, group, associate, federate and backup SharePoint services.
  • Can define specialized admin roles and can be delegated to users who are not farm admins.
  • Security trimmed

Security
  • Claims-based identity model

Example - Search service

Benefits

The Service Application Framework allows developers to provide load balanced middle-tier resources that can be managed through SharePoint and leverage the full power of SharePoint 2010.

NOTE: These notes are based on the Microsoft documentation on SharePoint 2010 and is subject to change.

SharePoint 2010 - Creating Web Application


IIS Manager

Each web application in SharePoint is configured along with IIS (Internet Information Services). Use the inetmgr command to view the IIS Manager as shown below:



The above highlighted applications represent the SharePoint port 80 web application and Central Application web application.

Steps in creating Web Application

Now we can create a web application in a SharePoint instance. The new web application can be used to support a new set of users. For creating it please use the following steps.

Open the Central Administration from the Start Menu and click on the Manage Web Applications item.



In the window shown you will see 2 items; the default web site and the Central Administration site. The default web site is created during SharePoint installation. The Central Administration is the website which is used by Administrators - it contains items to manage existing web sites, create new websites and other tasks. Click on the New button from the Ribbon UI.





Here you can set the following options:
  1. IIS Web site
  2. Application Pool
  3. Security
  4. Database Name

Leave the default values except in the Security Account in Application Pool.



Note: If you are get any error about Managed Account. You can register a new one see above link on image. 

Press the Ok button and you will see the following busy screen. Web Application creation is a time consuming process and may take several minutes.



Waiting for a while you can see that the new web application is created.



Please note the port number of our new web application.

SharePoint 2010 - Site Collections, Subsites, Top-Level Site

What is Site Collection?
A site collection is a group of web sites that have the same owner and share the same settings.

For example, a Document Site Collection can contain a Word Document Site and an Excel Document Site under it.

Please note that we should create a site collection inside a web application.

What is a Top Level Site?

A Site Collection can be divided into two:
  1. Subsites
  2. Top Level Site

Subsites are those sites under the site collection.

A Top Level site is a site not created under another site. A top level site can have subsites under it.

Note

All the contents of a site collection are stored inside the configured SQL Server database. We can add upto 15000 (configurable value) site collections inside the content database. Often the term sites is used to represent site collections.

Creating Site Collection

Now we can experiment with creating a site collection. Open the Central Administration web application through the start menu.



We are going to create a site collection for a group of Microsoft .Net users in the company.

Click on the Create site collections link as shown above. You will get the following screen.



Enter the details in the highlighted areas. The description of which is given below:

Item
Description
Web Application
Choose your web application.  By default there is only one web application.
Title
Title for your site collection, appears in browser
Description
Description of your site
Url
Enter the url suffix.  This url can be shared for the group of users.
User Name
The users who are allowed to access this site collection.  Use the Check Names option to auto complete the qualified user name after entering the user name.




On clicking the Ok button and waiting for a while the new site collection will get created. A message appears like shown below.



Click the Ok button to return to Central Administration.

Now you are ready with the new site collection. You can share the url between the intended users.

Opening the url in browser the following page was displayed.



Now the users of the site collection can create new sites inside this site collection. We can explore that in the next chapters.

References

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