Archive

Archive for the ‘Object Model Code’ Category

Sample Code to Load data from SharePoint List Using CamlQuery


http://microsoft-techies.blogspot.com/2014/03/sample-code-to-load-data-from.html

Advertisements

SharePoint Group same list data from different sites


Grouping same list data from different sites.

 

I have list name called News in all sub sites, I want to show all the lists data in one place using a grid.

Solution:

Create a visual webpart

Add a gridview control.

Add below code in ascx.cs file

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using System.Web;

using System.Data;

using Microsoft.SharePoint.Administration;

 

namespace Group.VisualWebPart1

{

public partial class VisualWebPart1UserControl : UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

DataTable table = new DataTable();

table.Columns.Add(“Site”, typeof(string));

table.Columns.Add(“Web”, typeof(string));

table.Columns.Add(“List”, typeof(string));

table.Columns.Add(“ItemURL”, typeof(string));

table.Columns.Add(“ItemTitle”, typeof(string));

table.Columns.Add(“Size”, typeof(string));

 

#region Only in Root Web

SPSecurity.RunWithElevatedPrivileges(delegate

{

SPSiteCollection siteCollections = SPContext.Current.Web.Site.WebApplication.Sites; //oSPWeb.WebApplication.Sites;

 

foreach (SPSite objSPSite in siteCollections)

{

SPWeb objSPWeb = objSPSite.RootWeb;

 

//Shared Documents should be available in all sites including sub sites.

SPList spDocument = objSPWeb.Lists[“News”];

foreach (SPListItem objItem in spDocument.Items)

{

try

{

//table.Rows.Add(objSPSite.Url, objSPWeb.Title, spDocument.Title, objItem.Url, objItem.Title, objItem.File.Length);

table.Rows.Add(objSPSite.Url, objSPWeb.Title, spDocument.Title, objItem.Title);

 

}

catch (Exception ec) { }

}

objSPWeb.Close();

 

}

});

#endregion

 

#region All Sites

 

//SPSecurity.RunWithElevatedPrivileges(delegate

//{

//    SPSiteCollection siteCollections = SPContext.Current.Web.Site.WebApplication.Sites; //oSPWeb.WebApplication.Sites;

 

//    foreach (SPSite objSPSite in siteCollections)

//    {

 

//        foreach (SPWeb objSPWeb in objSPSite.AllWebs)

//        {

//            //Shared Documents should be available in all sites including sub sites.

//            SPList spDocument = objSPWeb.Lists[“News”];

//            foreach (SPListItem objItem in spDocument.Items)

//            {

//                try

//                {

//                    //table.Rows.Add(objSPSite.Url, objSPWeb.Title, spDocument.Title, objItem.Url, objItem.Title, objItem.File.Length);

//                    table.Rows.Add(objSPSite.Url, objSPWeb.Title, spDocument.Title, objItem.Title);

 

//                }

//                catch (Exception ec) { }

//            }

//            objSPWeb.Close();

//        }

//    }

//});

 

#endregion

 

GridView1.DataSource = table;

GridView1.DataBind();

 

}

}

}

 

SharePoint 2010 Object Model Code


using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SampleOMC.COMwp
{
[ToolboxItemAttribute(false)]
public class COMwp : WebPart
{
protected override void CreateChildControls()
{
#region Object MOdel Code
#region NewSite
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//SPWebCollection myWebCol = myWeb.Webs;
//SPWeb mynewweb = myWebCol.Add(“NewSite”, “NewSite Title”, “NewSite Description”, 1033, “STS#0”, false, false);
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region ListCreate

//SPSite mySite = new SPSite(“http://SERVER:8888/NewSite”);
//SPWeb myWeb = mySite.OpenWeb();
//myWeb.AllowUnsafeUpdates = true;
//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();
//myWeb.AllowUnsafeUpdates = false;
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();

#endregion

#region Show All all subsites in site collection

//SPSite mySite = new SPSite(“http://SERVER:8888”);
//foreach (SPWeb myWeb in mySite.AllWebs)
//{
// Console.WriteLine(myWeb.Url.ToString());
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();

 

#endregion

#region Show all Roles in a site
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//foreach (SPRoleDefinition myRoleDef in myWeb.RoleDefinitions)
//{
// Console.WriteLine(myRoleDef.Name);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region Show all Alerts in a site
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//foreach (SPAlert myAlerts in myWeb.Alerts)
//{
// Console.WriteLine(myAlerts.Title);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region ShowAllList
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//foreach (SPList myList in myWeb.Lists)
//{
// Console.WriteLine(myList.Title.ToString());
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();

#endregion

#region ShowListTemplates

//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//foreach (SPListTemplate myListTemplate in myWeb.ListTemplates)
//{
// Console.WriteLine(myListTemplate.Name);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();

#endregion

#region ShowAllfieldsIn a List
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//SPList myList = myWeb.Lists[“List Name”];
//foreach (SPField myField in myList.Fields)
//{
// Console.WriteLine(myField.InternalName);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region List All Items in a Column
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//SPList myList = myWeb.Lists[“List Name”];
//SPQuery myQuery = new SPQuery();
//myQuery.Query = “”;//Your Query
//SPListItemCollection myItemCol = myList.GetItems(myQuery);
//foreach (SPListItem myListItem in myItemCol)
//{
// Console.WriteLine(myListItem[“Column Name”].ToString());
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region Delete all items from list
//SPSite mySite = new SPSite(“http://SERVER:8888”);
//SPWeb myWeb = mySite.OpenWeb();
//SPList myList = myWeb.Lists[“List Name”];
//myWeb.AllowUnsafeUpdates = true;
//int count = 1;
//for (int i = 0; i < myList.ItemCount; i++)
//{
// SPListItem myListitem = myList.Items[0];
// myListitem.Delete();
// Console.WriteLine(count + ” item(s) deleted”);
// count++;
//}
//myWeb.AllowUnsafeUpdates = false;
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();

#endregion

#region Show All Groups

//SPSite mySite = new SPSite(“http://SERVER:8888&#8221;);
//SPWeb myWeb = mySite.OpenWeb();
//foreach (SPGroup myGroup in myWeb.Groups)
//{
// Console.WriteLine(myGroup.Name);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region ShowAllUsers in a Group

//SPSite mySite = new SPSite(“http://SERVER:8888&#8221;);
//SPWeb myWeb = mySite.OpenWeb();
//SPGroup myGroup = myWeb.Groups[“Group Name”];
//foreach (SPUser myUser in myGroup.Users)
//{
// Console.WriteLine(myUser.Name);
//}
//Console.WriteLine(“Press any key to continue…..”);
//Console.ReadLine();
#endregion

#region Show all Users in a group from UserCollection
SPSite mySite = new SPSite(“http://SERVER:8888&#8221;);
SPWeb myWeb = mySite.OpenWeb();
SPUserCollection myUserCollection = myWeb.Groups[“My Visitors”].Users;
foreach (SPUser myUser in myUserCollection)
{
Console.WriteLine(myUser.LoginName);
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();

#endregion

#endregion
}
}
}

SharePoint 2010 Client Object Model

SharePoint 2010 SPUser


SharePoint 2010 SPUser

SharePoint SPUser

MOSS Run With ElevatedPrivileges


If you’re doing any sort of custom development within the MOSS framework, there might come a time when you’ll need to execute your code with elevated permissions. Meaning, instead of your code running under the context of the current user, you want to run it using an account with a higher level of access.
Within MOSS, there is a method that will do the trick. The RunWithElevatedPrivileges method will run under the context of the Application Pool Identity account.
As an example: The code below was taken from an application page I developed recently. When running this code using my credentials, it executed without trouble. Once logged in as a user with Contribute access, I was running into an Access Denied SharePoint error.
1
2
3
4
5 void DoSubmit(object sender, EventArgs e)
{
SPSite site = SPControl.GetContextSite(Context);
SPWebCollection webs = site.AllWebs;
}
My next step was to attempt to run the DoSubmit method using elevated privileges as follows.

void DoSubmit(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(GetExpiredPages);
}

void GetExpiredPages()
{
SPSite site = SPControl.GetContextSite(Context);
SPWebCollection webs = site.AllWebs;
}
The above code was still producing an Access Denied error. It took me a moment to realize what I was doing wrong. The trick was that I needed to create a NEW SPSite object INSIDE the function that I am trying to run using elevated privileges. If I don’t recreate the SPSite object, I’ll essentially still be executing my code under the context of the current user.

void GetExpiredPages()
{
SPSite site = new SPSite(SPControl.GetContextSite(Context).ID);
SPWebCollection webs = site.AllWebs;
}

 

Server and Site Architecture: Object Model Overview



http://www.infoq.com/articles/SharePoint-Andreas-Grabner

Categories: Object Model Code