Microsoft Most Valuable Professional

8 January, 2010 (16:32) | Announcements | By: G Vijai Kumar

MVP Logo

MVP Logo

Happy to inform you that, on 1st January 2010 I have received a mail from Microsoft that I have been awarded as Microsoft Most Valuable Professional (MVP) in Sharepoint Server. What a surprise! I am so thrilled, thank you so much MICROSOFT for recognizing my contributions and honoring me with MVP award

The mail I received from Microsoft

Dear G Kumar,

Congratulations! We are pleased to present you with the 2010 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. We appreciate your outstanding contributions in SharePoint Server technical communities during the past year.

Also in this email:

* About your MVP Award Gift
* How to access www.mvpaward.com to begin taking advantage of your award benefits
* Your MVP Identification Number
* MVP Award Program Code of Conduct

The Microsoft MVP Award provides us the unique opportunity to celebrate and honor your significant contributions and say “Thank you for your technical leadership.”

Toby Richards
General Manager
Community & Online Support

***********************************************************************************************************

Yet another Sharepoint sample site template

12 November, 2009 (12:51) | MOSS - Customization with SPD, MOSS - Master Pages, MOSS - Site Templates | By: G Vijai Kumar

Here I created a simple sample Sharepoint site template with rounded corners.

This template allows you to create a new site with rounded corners around the webparts

Sharepoint Sample Master Page

Sharepoint Sample Site Template

Download the site template

Step by step installation:

  • Log into your SharePoint site
  • Go to Site Settings, under Galleries click on Site Template
  • Then, click on Upload to upload the site template on to the site
  • Browse to the downloaded file and click Open
  • Click Ok to complete the upload process and now the template is available for you to use

Step by step creating of site:

  • Log into your Sharepoint site
  • Go to Site Settings, under Site Administration section click on Sites and Workspaces
  • On Sites and Workspaces page click on Create for creating a new site
  • Provide the necessary information, then select Custom tab
  • You can see the uploaded template listed select the template 5NumberSiteTemplate_Pink.stp
  • Finally, click on Create

Sharepoint list form generator

14 October, 2009 (15:49) | MOSS - Object Model | By: G Vijai Kumar

Today I am going to show you how to generate a list form dynamically as soon as you select the list name you can see the form generated for you with file upload field (attachment). I also know that there is  an excellent solution Sharepoint Form Generator developed by Alon Havivi, but still I want to share with you, so that for some one the code may be helpful as a whole or part of it

No problem whether your list contains 1 field or 100 fields, code will  generate all the ‘n’ no.of fields with file upload field (attachment) contained in the list. It loops through all the input fields and creates at runtime.

All you have to do is, open visual studio 2005/2008 create a new project using webpart template use the code from WebPart1.cs and FormGeneratorToolPart.cs build the project, place the .dll file in GAC or site bin directory, add the necessary safe control tag in Web.config file, import the webpart in to gallery, and add the same on to your site, after adding the webpart in your site you have to select the list name from the webpart properties, so that you can see the generated form similar to the form as in NewForm.aspx

Select the list name from the list of lists

Select the list name from the list of lists

List form generated

List form generated

Download the complete source code (Please Note: Code cannot be viewed properly on the web page, so please use the below links to view the code or for downloading)

WebPart1.cs
FormGeneratorToolPart.cs

WebPart1.cs

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

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

namespace FormGenerator
{
    [Guid("93612d52-737c-4e83-83c3-a228910e87a3")]
    public class FormGenerator : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Table oFormTable;
        FileUpload oFormFileUpload;
        FieldLabel oFormLabelField;
        FormField oFormField;
        Button oFormButtonSubmit;
        Label oFormLabelMessage;

        SPList oFormList;

        private string _FormList = string.Empty;

        string qstitle = string.Empty;
        string qsmission = string.Empty;

        public string FormList
        {
            get { return _FormList; }
            set { _FormList = value; }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            oFormTable = new Table();
            oFormTable.CellPadding = 0;
            oFormTable.CellSpacing = 0;            

            oFormLabelMessage = new Label();
            oFormLabelMessage.ID = "lbl_message";
            oFormLabelMessage.CssClass = "ms-formvalidation";
            this.Controls.Add(oFormLabelMessage);

            //Generate Form
            GenerateFormList();
        }

        private void GenerateFormList()
        {
            SPWeb oWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
            if (FormList != "")
            {
                try
                {
                    oFormList = oWeb.Lists[FormList];
                    foreach (SPField oField in oFormList.Fields)
                    {
                        if (oField.Type == SPFieldType.Attachments)
                        {
                            FieldLabel oLabelAttachmentField = new FieldLabel();
                            oLabelAttachmentField.ControlMode = SPControlMode.New;
                            oLabelAttachmentField.ListId = oFormList.ID;
                            oLabelAttachmentField.FieldName = oField.InternalName;

                            oFormFileUpload = new FileUpload();
                            oFormFileUpload.ID = "FileUpload";

                            AttachmentsField oAttachmentField = new AttachmentsField();
                            oAttachmentField.ControlMode = SPControlMode.New;
                            oAttachmentField.ListId = oFormList.ID;
                            oAttachmentField.FieldName = oField.InternalName;
                            oAttachmentField.Controls.Add(oFormFileUpload);
                            oAttachmentField.ID = "Control_" + oField.InternalName;

                            TableRow oRowAttachment = new TableRow();
                            oFormTable.Rows.Add(oRowAttachment);
                            TableCell oCellAttachmentLabel = new TableCell();
                            oRowAttachment.Cells.Add(oCellAttachmentLabel);

                            oCellAttachmentLabel.Controls.Add(oLabelAttachmentField);
                            oCellAttachmentLabel.CssClass = "ms-formlabel";
                            TableCell oCellAttachment = new TableCell();
                            oRowAttachment.Cells.Add(oCellAttachment);
                            oCellAttachment.Controls.Add(oAttachmentField);
                            oCellAttachment.CssClass = "ms-formbody";
                        }
                    }

                    // Loop through all the fields in the list
                    foreach (SPField oField in oFormList.Fields)
                    {
                        // Avoid Hidden, Read Only, Attachments field
                        if (!oField.Hidden && !oField.ReadOnlyField && oField.Type != SPFieldType.Attachments)
                        {
                            oFormLabelField = new FieldLabel();
                            oFormLabelField.ControlMode = SPControlMode.New;
                            oFormLabelField.ListId = oFormList.ID;
                            oFormLabelField.FieldName = oField.InternalName;

                            oFormField = new FormField();
                            oFormField.ControlMode = SPControlMode.New;
                            oFormField.ListId = oFormList.ID;
                            oFormField.FieldName = oField.InternalName;
                            oFormField.ID = "Control_" + oField.InternalName;

                            TableRow oRow = new TableRow();
                            oFormTable.Rows.Add(oRow);

                            TableCell oCellLabel = new TableCell();
                            oRow.Cells.Add(oCellLabel);
                            TableCell oCellControl = new TableCell();
                            oRow.Cells.Add(oCellControl);

                            oCellLabel.Controls.Add(oFormLabelField);
                            oCellControl.Controls.Add(oFormField);

                            oCellLabel.CssClass = "ms-formlabel";
                            oCellControl.CssClass = "ms-formbody";
                        }
                    }

                    //Create ASP.Net button
                    oFormButtonSubmit = new Button();
                    oFormButtonSubmit.ID = "btn_submit";
                    oFormButtonSubmit.Text = "OK";
                    oFormButtonSubmit.CssClass = "ms-ButtonHeightWidth";
                    oFormButtonSubmit.Click += new EventHandler(oFormButtonSubmit_Click);
                    this.Controls.Add(oFormButtonSubmit);

                    // Create the row for the Submit button
                    TableRow oRowButton = new TableRow();
                    oFormTable.Rows.Add(oRowButton);

                    // Create the cell for the Submit button
                    TableCell oCellButton = new TableCell();
                    oCellButton.ColumnSpan = 2;
                    oRowButton.Cells.Add(oCellButton);

                    Controls.Add(oFormTable);
                }
                catch (Exception ex)
                {
                    Page.Response.Write(ex.ToString());
                }
            }
            else
            {
                Page.Response.Write("Select valid List from webpart properties");
            }
        }

        void oFormButtonSubmit_Click(object sender, EventArgs e)
        {
            SPSite mySite = SPControl.GetContextSite(Context);
            SPWeb myWeb = SPControl.GetContextWeb(Context);
            SPList myList = myWeb.Lists[FormList];
            SPListItem myItem = myList.Items.Add();

            //Validating the controls
            foreach (SPField sField in myList.Fields)
            {
                if (sField.Required == true)
                {
                    string oControl = "Control_" + sField.InternalName.ToString();
                    Control oFieldControl = this.FindControl(oControl);
                    FormField sFormField = (FormField)oFieldControl;
                    if ((sFormField.Value) == null || (sFormField.Value.ToString()) == "")
                    {
                        oFormLabelMessage.Visible = true;
                        oFormLabelMessage.Text = "* indicates required fields";
                        return;
                    }
                }
            }
            foreach (SPField oField in myList.Fields)
            {
                string oFieldID = "Control_" + oField.InternalName;
                Control sFieldControl = this.FindControl(oFieldID);
                if (sFieldControl != null)
                {
                    if (oField.Type != SPFieldType.Attachments)
                    {
                        FormField sFormField = (FormField)sFieldControl;
                        myItem[oField.InternalName] = sFormField.Value;
                    }
                    else
                    {
                        AttachmentsField sAttachmentField = (AttachmentsField)sFieldControl;
                        FileUpload oFileControl = (FileUpload)sAttachmentField.FindControl("FileUpload");
                        if (oFileControl != null)
                        {
                            try
                            {
                                HttpPostedFile oPostedFile = oFileControl.PostedFile;
                                string oFileName = Path.GetFileName(oPostedFile.FileName);
                                if (oPostedFile.ContentLength > 0)
                                {
                                    Stream oInputStream = oPostedFile.InputStream;
                                    byte[] oBT = new byte[oPostedFile.InputStream.Length];
                                    oPostedFile.InputStream.Seek(0, SeekOrigin.Begin);
                                    oPostedFile.InputStream.Read(oBT, 0, oBT.Length);
                                    myItem.Attachments.Add(oFileName, oBT);
                                }
                            }
                            catch (Exception ex)
                            {
                                oFormLabelMessage.Visible = true;
                                oFormLabelMessage.Text = ex.ToString();
                            }
                        }
                    }
                }
                myWeb.AllowUnsafeUpdates = true;
                myItem.Update();
                myWeb.AllowUnsafeUpdates = false;
                oFormLabelMessage.Visible = true;
                oFormLabelMessage.Text = "Record updated";
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (FormList != "")
            {
                writer.Write("
"); oFormLabelMessage.RenderControl(writer); writer.Write("
"); writer.Write("
"); writer.Write(""); writer.Write("
"); oFormTable.RenderControl(writer); writer.Write("
"); oFormButtonSubmit.RenderControl(writer); writer.Write("
"); } else { oFormLabelMessage.RenderControl(writer); } } public override ToolPart[] GetToolParts() { ToolPart[] allToolParts = new ToolPart[3]; WebPartToolPart standardToolParts = new WebPartToolPart(); CustomPropertyToolPart customToolParts = new CustomPropertyToolPart(); allToolParts[0] = standardToolParts; allToolParts[1] = customToolParts; allToolParts[2] = new FormGeneratorToolPart(); return allToolParts; } } }

FormGeneratorToolPart.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace FormGenerator
{
    class FormGeneratorToolPart: Microsoft.SharePoint.WebPartPages.ToolPart
    {
        FormGenerator oFG;
        Panel oToolPartPanel;
        DropDownList oDDLListProvider;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            oToolPartPanel = new Panel();
            Controls.Add(oToolPartPanel);

            oDDLListProvider = new DropDownList();
            oToolPartPanel.Controls.Add(oDDLListProvider);

            PopulateProviderList();
        }

        private void PopulateProviderList()
        {
            SPListCollection myListCol = SPContext.Current.Web.Lists;
            oDDLListProvider.AppendDataBoundItems = true;
            foreach (SPList myList in myListCol)
            {
                ListItem myListItem = new ListItem(myList.Title, myList.Title);
                oFG = (FormGenerator)this.ParentToolPane.SelectedWebPart;
                if (oFG.FormList == myList.Title)
                    myListItem.Selected = true;
                oDDLListProvider.Items.Add(myListItem);
            }
        }

        public override void ApplyChanges()
        {
            base.ApplyChanges();
            oFG.FormList = oDDLListProvider.SelectedValue;
        }

        public override void SyncChanges()
        {
            base.SyncChanges();
            oDDLListProvider.SelectedValue = oFG.FormList.ToString();
        }

        protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)
        {
            output.Write("List Name Lookup:");
            output.Write("

"); oDDLListProvider.RenderControl(output); output.Write("
 

"); } } }

A quick look on WSS Out Of Box web services

14 October, 2009 (07:40) | Web Services | By: G Vijai Kumar

Here I am going to show you how to create/delete site collection and create/delete list using WSS OOB webservices

Creating site collection

static void Main(string[] args)
{

AdminService.Admin admService = new AdminService.Admin();
admService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
admService.CreateSite("http://fivenumber:5/sites/someothername", "SomeotherTitle", "SomeotherDescription", 1033, "STS#0", "Fivenumber\\g.vijaikumar", "GVijaiKumar", "g.vijaikumar@fivenumber.com", "", "");
Console.WriteLine("Site Collection Created:");
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine(ex);
}

Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}

Deleting site collection

static void Main(string[] args)
{

AdminService.Admin admService = new AdminService.Admin();
admService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
admService.DeleteSite("http://fivenumber:5/sites/someothername");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

Creating List

static void Main(string[] args)
{

List50.Lists listService = new List50.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
XmlNode ndList = listService.AddList("List_Name", "List_Description", 100);
Console.WriteLine("List Created:");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

Deleting List

static void Main(string[] args)
{

List50.Lists listService = new List50.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
listService.DeleteList("List_Name");//, "List_Description", 100);
Console.WriteLine("List Deleted:");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

A quick look on Sharepoint object model programs

24 September, 2009 (13:11) | MOSS - Object Model, MOSS - Quick Look | By: G Vijai Kumar

Here I am going to show you that the actions which we perform normally with UI, those also can be done programmatically, the same thing I am going to show in this post, this post is mainly targeted for beginners those who are new to Sharepoint object model, a quick watch on programs to create sub sites, lists, showing web apps etc.
Creating Sub Site:

static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servernam:port");
SPWeb myWeb = mySite.OpenWeb();
SPWebCollection myWebCol = myWeb.Webs;
SPWeb mynewweb = myWebCol.Add("Web url", "Web Title", "Web Description", 1033, "STS#0", false, false);
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}

Creating List:

static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servernam:port");
SPWeb myWeb = mySite.OpenWeb();
myWeb.Lists.Add("My New List", "My new list description", myWeb.ListTemplates["Custom List"]);
SPList newList = myWeb.Lists["My New List"];
newList.OnQuickLaunch = true;
newList.Update();
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}

Show all top-level sites in a farm

static void Main()        
 {
 foreach (SPWebApplication myWebApp in SPWebService.ContentService.WebApplications)
 {
 foreach (SPSite mySiteCol in myWebApp.Sites)
 {
 try
 {
 Console.WriteLine(mySiteCol.Url);
 }
 catch (Exception e)
 {
 Console.WriteLine(e);
 }                   
 }
 }
 Console.WriteLine("Press any key to continue.....");
 Console.ReadLine();
 }

Show all site collection in web application:

static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servernam:port");
SPWebApplication myWebApp = mySite.WebApplication;
SPSiteCollection mySiteCol = myWebApp.Sites;
foreach (SPSite SingleSite in mySiteCol)
{
Console.WriteLine(SingleSite.Url.ToString());
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}

Show all subsites in site collection:

static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servernam:port");
foreach (SPWeb myWeb in mySite.AllWebs)
{
Console.WriteLine(myWeb.Url.ToString());
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}

Show all Lists in a site:

static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servernam:port");
SPWeb myWeb = mySite.OpenWeb();
foreach (SPList myList in myWeb.Lists)
{
Console.WriteLine(myList.Title.ToString());
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}