Pages

Sunday, 24 August 2014

Display SharePoint List Items in Sharepoint Web Part Using Programmatically

The code to accomplish this is quite easy, you add a web part that is displaying data for a list,


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

// Get a reference to a web and a list
SPSite site = new SPSite("http://localhost:8000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Contacts"];

// Instantiate the web part
ListViewWebPart wp = new ListViewWebPart();
wp.ZoneID = "Left";
wp.ListName = list.ID.ToString("B").ToUpper();
wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

// Get the web part collection
SPWebPartCollection coll = 
 web.GetWebPartCollection("default.aspx", 
 Storage.Shared);

// Add the web part
coll.Add(wp); 

First you get a reference to the SPWeb in which you want to add the web part, and to the list you want to use (in this example the Contacts list). Next you create an instance of the ListViewWebPart class, in which you can set the ZoneID, the ListName and the ViewGuid. This is the tricky part, the ListName property should contain the ID of your list (a GUID), not the name of your list!! But the ListName property is of the type string, so you need to convert the List GUID to a string using .ToString(“B”).ToUpper(). The same goes for the ViewGuid. Finally you need to get a reference to the WebPartCollection for the page in which you want to add the web part (in this example the home page, being default.aspx). Now you can add the web part using the Add method.

Saturday, 23 August 2014

How to resolve "The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)"

In this article we are going to see how to resolve "The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) "for console application in Visual Studio 2010.


In this article we are going to see how to resolve "The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) "For console application in Visual Studio 2010. Even though if you add the Reference Microsoft.SharePoint.dll or Microsoft.SharePoint.Client.dll in your console application and if you try to build your application, you will be getting the above error.



I have created one Console application using Visual Studio 2010 and I have added the reference Microsoft.SharePoint.dll or Microsoft.SharePoint.Client.dll.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace ClientObjectModelIntroduction
{
    class Program    {
        static void Main(string[] args)
        {
            ClientContext site = new ClientContext("http://abc/");
            
          SPWeb web= new SPSite("http://abc/").OpenWeb();
          
        }
    }
}

When you hit the F5 you will be getting the follownig error.

image2.gif

How to resolve this error:
  • Go to the Solution explorer.
  • Right click on the Solution Explorer and click Properties.
  • Select Application Tab, by default Target Framework will be .Net Framework 3.5 Client Profile change it to .Net Framework 3.5.


    image3.gif
  • Once you change the Target Framework "Target Framework Change "wizard will pop up.
  • Click Yes.
  • Go the Build tab and change the Platform Target to x64.
  • Hit F5 now you will be able to debug successfully.

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.