Pages

Tuesday, 13 September 2011

Deleting SharePoint list items Using Console Application Programming and Object Models

Click here to Download Document for this post
In this post we will learn how to Deleting SharePoint List items.

Here in this scenario I have chosen Console Application template because it doens’t contains any input fields to show as webpart and also to avoid GAC registration, safe controls etc., I felt it will be easy to execute the code in Console.

Click here to Download code

See below code snippet

// Deleting SharePoint list items Using Console Application Programming and Object Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace DeleteListItem

{
    class DeletingListItem
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://URl/rf"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    int count = 0;
                    SPList mySourceList = web.Lists["NewEmpMasterList"];
                    foreach (SPListItem item in mySourceList.Items)
                    {
                        try
                        {
                            string EmpId = item["Employee ID"].ToString();
                            string EmpName = item["Employee name"].ToString();
                            string active = item["Active"].ToString();
                            string stages = item["Stages"].ToString();
                            if (active == "No" || stages == "Stage 4")
                            {
                                try
                                {
                                    item.Delete();
                                    item.Update();                                    
                                }
                                catch (Exception){}
                                count++;
                                Console.WriteLine(count + "  item(s) Deleted  " + EmpId);
                            }
                        }
                        catch (Exception){}
                    }
                }
            }
        }
    }
}


run the program 

This no of Items to be deleted for my SharePoint List


Copy SharePoint List items(selected fields) from Source List to another List(Destination List), using programmatically


In this post we will learn how to copy list items from one list to another list using Console Application Program.

I have created two Sharepoint sites, first is(source Site) http://URl/rf and the second(destination site) http://URl/rf

I have also created custom list in each site, Source List in site http://URl/rf and Desitination List in http://URl/rf

Create two custom lists Source List and destination List:
Source List Name "NewEmpMasterList" (below are columns and column types)
Employee Id (Single line of text)
Employee Name (Single line of text)
Stages (Choice),Department .Designation ,Date of joining ,Official email ID ,Date of birth ,                               Approver Name ,Permanent address ,Address for communication.........................ext
Destination List "Exit EMP List" (below are columns and column types)
Employee Id (Single line of text)
Employee Name (Single line of text)
Stages (Choice),Department .Designation ,Date of joining ,Official email ID ,Date of birth ,                               Approver Name ,Permanent address ,Address for communication.........................ext
After creating the two lists:
Here in this scenario I have chosen Console Application template because it doens’t contains any input fields to show as webpart and also to avoid GAC registration, safe controls etc., I felt it will be easy to execute the code in Console.

If you have Lookup column in your Source List and want to copy the same data in to Destination List, you have to create an instance for SPFieldLookupValue class like below.

Let’s suppose ‘Employee Name’ column is lookup field, comment or remove the line(string SEmpName = item["Employee Name"].ToString();)  in the below code and replace with below code

SPFieldLookupValue SLookupEmpName = new SPFieldLookupValue(item["Employee Name"].ToString()); 
string SEmpName = SLookupEmpName.LookupId.ToString();

Click here to Download Code

//Copy Sharepoint list items(selected fields) from Source List to Destination List programmatically(Rajkumar kaki)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace CopyEMPListItems
{
    class CopyEmployeeMasterList
    {
        static void Main(string[] args)
        {
            //Source Site
            using (SPSite site = new SPSite("http://URl/rf"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    int count = 0;
                    //Source List Name
                    SPList mySourceList = web.Lists["NewEmpMasterList"];
                    foreach (SPListItem item in mySourceList.Items)
                    {
                        try
                        {
                            string active = item["Active"].ToString();
                            string stages = item["Stages"].ToString();
                            if (active == "No" || stages == "Stage 4")
                            {
                                try
                                {
                                    string SourceEmpId = item["Employee ID"].ToString();
                                    string SEmpName = item["Employee name"].ToString();
                                    string SDepartment = item["Department"].ToString();
                                    string SDesi = item["Designation"].ToString();
                                    string SDOJ = item["Date of joining"].ToString();
                                    string SEmailID = item["Official email ID"].ToString();
                                    string SDOB = item["Date of birth"].ToString();
                                    string SApproverName = item["Approver Name"].ToString();
                                    string SPerAddress = item["Permanent address"].ToString();
                                    string SAForCommu = item["Address for communication"].ToString();                                  
                                    if (item["Category"] == null)
                                    {
                                        if (SDepartment == "BPO")
                                            item["Category"] = "BPO";
                                        if (SDepartment == "US Payroll" || SDepartment == "US Staffing")
                                            item["Category"] = "US";
                                        else item["Category"] = "Development";
                                    }
                                    string SCategory = item["Category"].ToString();

                                    if (item["Employee base"] == null)
                                    {
                                        if (SDepartment == "BPO")
                                            item["Employee base"] = "ADC";
                                        else item["Employee base"] = "KDC";
                                    }
                                    string SEmpBase = item["Employee base"].ToString();
                                    if (item["Stages"] == null)
                                        item["Stages"] = "Stage 4";
                                    string SStages = item["Stages"].ToString();
                                    if (item["Contact number"] == null)
                                        item["Contact number"] = "0000000000";
                                    string SContactNo = item["Contact number"].ToString();
                                    if (item["Current designation"] == null)
                                        item["Current designation"] = "Exit Employee";
                                    string SCurDesi = item["Current designation"].ToString();
                                    if (item["Education qualification"] == null)
                                        item["Education qualification"] = "Exit Employee";
                                    string SEducQua = item["Education qualification"].ToString();
                                    if (item["Approver Email"] == null)
                                        item["Approver Email"] = SApproverName + "@preludesys.com";
                                    string SAppEmail = item["Approver Email"].ToString();
                                    if (item["Approver Emp ID"] == null)
                                        item["Approver Emp ID"] = SApproverName;
                                    string SAppEmpID = item["Approver Emp ID"].ToString();
                                    if (item["Active"] == null)
                                        item["Active"] = "No";
                                    string SActive = item["Active"].ToString();
                                    if (item["Date of confirmation"] == null)
                                        item["Date of confirmation"] = SDOJ;
                                    string SDOC = item["Date of confirmation"].ToString();
                                    if (item["Casual Leave"] == null)
                                        item["Casual Leave"] = "0";
                                    string SCasualL = item["Casual Leave"].ToString();
                                    if (item["Father Name"] == null)
                                        item["Father Name"] = SEmpName;
                                    string SFatherName = item["Father Name"].ToString();
                                    //Destination Site
                                    using (SPSite myDestinationSite = new SPSite("http://URl/rf"))
                                    {
                                        using (SPWeb myDestinationWeb = myDestinationSite.OpenWeb())
                                        {
                                            try
                                            {
                                                //Destination List Name
                                                SPList myDestinationList = myDestinationWeb.Lists["Exit EMP List"];
                                                SPListItem myDestinationListItem = myDestinationList.Items.Add();
                                                myDestinationListItem["Employee ID"] = SourceEmpId;                                               
                                                myDestinationListItem["Employee name"] = SEmpName;
                                                myDestinationListItem["Category"] = SCategory;
                                                myDestinationListItem["Department"] = SDepartment;
                                                myDestinationListItem["Designation"] = SDesi;
                                                myDestinationListItem["Date of joining"] = SDOJ;
                                                myDestinationListItem["Stages"] = SStages;
                                                myDestinationListItem["Official email ID"] = SEmailID;
                                                myDestinationListItem["Contact number"] = SContactNo;
                                                myDestinationListItem["Employee base"] = SEmpBase;
                                                myDestinationListItem["Date of birth"] = SDOB;
                                                myDestinationListItem["Current designation"] = SCurDesi;
                                                myDestinationListItem["Education qualification"] = SEducQua;
                                                myDestinationListItem["Approver Email"] = SAppEmail;
                                                myDestinationListItem["Approver Emp ID"] = SAppEmpID;
                                                myDestinationListItem["Active"] = SActive;
                                                myDestinationListItem["Date of confirmation"] = SDOC;
                                                myDestinationListItem["Father Name"] = SFatherName;
                                                myDestinationListItem["Casual Leave"] = SCasualL;
                                                myDestinationListItem["Approver Name"] = SApproverName;
                                                myDestinationListItem["Address for communication"] = SAForCommu;
                                                myDestinationListItem["Permanent address"] = SPerAddress;
                                                myDestinationWeb.AllowUnsafeUpdates = true;
                                                myDestinationListItem.Update();
                                                myDestinationWeb.AllowUnsafeUpdates = false;
                                                //Counting No Of List Items
                                                count++;
                                                Console.WriteLine(count + " item(s) copied");
                                            }
                                            catch (Exception){}
                                        }
                                    }
                                }
                                catch (Exception) {}
                            }
                        }
                        catch (Exception) {}
                    }
                }
            }
        }
    }
}

ofter run the program

 

ofter run the program in my Destination List 383 items copied form Source List

Tuesday, 6 September 2011

What is the difference between a Blog and a Wiki

Hai

Click here to Download Document for this Post

Blogs:

  1. A blog is a sort of online journal. It is updated daily or weekly or whenever the author desires.
  2. A blog is owned by an individual.
  3. The timeline is more important. 
  4. Blog posts are usually one persons' opinion, followed optionally by comments.
  5. A blog is a Web site that maintains ongoing posts.
  6.  A blog is frequently updated,a personal Web site featuring diary-type commentary and links to articles or other Web sites. 
  7. Blogs- Usually a single author. Sometimes can have multiple contributors, 
    - Author posts, user comments, 
    - Opinion Sharing , 
    - One-to-many content. 
Wiki:
  1. A wiki is a platform that is meant for anyone to update in real time.
  2. wiki is being updated by many people around the world.
  3. For wikis, when an article was published matters less, because articles are supposed to be updated as new information becomes available.
  4. Wiki articles represent consensus, but can have an associated discussion/talk page. 
  5. A wiki is a superset of a blog. A wiki can host a blog, but not viceversa. Too emulate a blog in a wiki, the wiki page needs to be protected against editing by other users than the author, and a comment widget (e.g. Disqus) needs to be added to the page. 
  6. A wiki allows multiple users to create, modify and organize web page content in a collaborative manner. 
  7. Wikis- Multiple Authors, 
    - Edited by a group or team, 
    - Contains links to other Wiki pages, 
    - Continuousy changing and growing rapidly, 
    - Many-to-many communication.