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. 

Wednesday, 17 August 2011

SharePoint List Items Update Using Console Application Programming and Object Models (Adding number to all items).

Click here to Download Document for this Post
In this post we will learn how to SharePoint List Items Update Using Console Application Programming and Object Models (Adding number to all Columns).
1.Calculating no of Months form Employee Date of Joining.

2.Calculating no of Days from Employee Date of Joining.(With including Saturday and Sunday).
3.Calculating no of Days from Employee Date of Joining.( Without including Saturday and Sunday).
4.In this programs converting string to DateTime and string to Double also.
Steps for SharePoint List Items Update Using Console Application Programming and Object Models.
step 1.Createing List "EMP Master List" Like bellow.
       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.
 
Step2. Creating console Application using visual studio.
Step3. Add microsoft sharepoint reference
and Namespace using Microsoft.SharePoint
-->This is Total Program.
Click here to Download code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Casual_Leave_Updating
{
    class CasualLeaveUpdating
    {
        static void Main(string[] args)
        {
            int icount = 0, count = 0;
            using (SPSite site = new SPSite("http://kapples:2931/sites/PTC/TS"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["EMP Master List"];
                    web.AllowUnsafeUpdates = true;                   
                    DateTime ToDayDate = DateTime.Now;
                    foreach (SPListItem item in list.Items)
                    {
                        try
                        {
                            string strActive = item["Active"].ToString();
                            DateTime DateOfJoining = Convert.ToDateTime(item["Date of joining"]);

                            //If User is Active Satate=Yes then EMP Master List is Update
                            if (strActive == "Yes")
                            {
                                //Calculating no of Month form Date of Joining (Employee Joining Date)
                                int iNoOfMonths = 12 * (ToDayDate.Year - DateOfJoining.Year) + ToDayDate.Month - DateOfJoining.Month;
                                Console.WriteLine("\nNo Of Months:" + iNoOfMonths);

                                //Calculating no of Days from Date of Joining (Employee Joining Date)(With including Saturday and Sunday)
                                TimeSpan tNoOfDays = ToDayDate.Subtract(DateOfJoining);
                                Console.WriteLine("\nNo Of Days" + tNoOfDays.TotalDays);

                                //Calculating no of Days from Date of Joining (Employee Joining Date)( Without including Saturday and Sunday)
                                while (DateOfJoining <ToDayDate)
                                {
                                    if (DateOfJoining.DayOfWeek != DayOfWeek.Saturday && DateOfJoining.DayOfWeek != DayOfWeek.Sunday)
                                        icount++;
                                    DateOfJoining = DateOfJoining.AddDays(1);
                                }
                                Console.WriteLine("\nNo Of Days Without including Saturday and Sunday:" +icount);
                                icount = 0;

                                //Updating EMP Master List items,if Employee Joining more then 6 months List Items is Updated.
                                if (iNoOfMonths >= 6)
                                {
                                    //If Casual Leave is 0 or null it's added 2
                                    if (item["Casual Leave"] == null)
                                    {
                                        item["Casual Leave"] = "2.0";
                                        item.Update();
                                    }
                                    else
                                    {
                                        try
                                        {
                                            double casualLeaves = Convert.ToDouble(item["Casual Leave"].ToString());
                                            casualLeaves = casualLeaves + 2.0;
                                            string casualLeaveStr = casualLeaves.ToString();
                                            item["Casual Leave"] = casualLeaveStr;
                                            item.Update();
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.Write("ex.message");
                                        }
                                    }

                                    count++;
                                }
                            }
                            list.Update();
                            //Console.WriteLine(item["Casual Leave"]);
                        }
                        catch (Exception)
                        {
                            //throw;
                        }

                    }
                    Console.WriteLine(count);
                    web.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
    }

}
-->Before "EMP Master List" List Item Updating see above image.
-->Ofter Run the Program "EMP Master List" List Item Updating.
//Updating EMP Master List items, if Employee Joining more then 6 months List Items is Updated
--> In List Casual Leave Columns adding 2 for each items