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 webnew 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.