Showing posts with label sharepoint list item. Show all posts
Showing posts with label sharepoint list item. Show all posts

Monday, November 28, 2011

Getting large no of items from SharePoint list

If you have to reterive a large number of Items and also need a better performance then you should use one of the methods below :

1. Using SPQuery 

2. Using PortalSiteMapProvider Class


Lets see the examples for both the methods :
Our Query - Query to get all the Items in a list where Category is "Sp2007"

SPQuery -


// Get SiteColl
SPSite curSite = new SPSite("http://myPortal");

//Get Web Application
SPWeb curWeb = curSite.OpenWeb(); 

// Create a SPQuery Object
SPQuery curQry = new SPQuery(); 

// Write the query
curQry.Query = "<Where><Eq><FieldRef Name='Category'/>
<Value Type='Text'>SP2007 </Value></Eq></Where>";

// Set the Row Limit
curQry.RowLimit = 100;

//Get the List 
SPList curList = curWeb.Lists(new Guid("myListGUID")); 

//Get the Items using Query
SPListItemCollection curItems = curList.GetItems(curQry); 

// Enumerate the resulting items 
foreach (SPListItem curItem in curItems) 
{

string ResultItemTitle = curItem["Title"].ToString();

}


PortalSiteMapProvider class -
The class includes a method calledGetCachedListItemsByQuery that retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call.
The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list, stores the results in cache and returns them from the method call.


// Get Current Web
SPWeb curWeb = SPControl.GetContextWeb(HttpContext.Current);

//Create the Query
SPQuery curQry = new SPQuery();
curQry.Query = "<Where><Eq><FieldRef Name=\'Category\'/><Value Type=\'Text\'>SP2007</Value></Eq></Where>";


// Get Portal Map Provider 
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;

PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb.ServerRelativeUrl), PortalWebSiteMapNode);

// Get the items
pItems = ps.GetCachedListItemsByQuery(pNode, "myListName_NotID", curQry, curWeb);

// Enumerate all resulting Items
foreach (PortalListItemSiteMapNode curItem in pItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}

Sunday, November 13, 2011

Access, Copy, and Move Files


To retrieve files and folders from a Web site, use the GetFile or GetFolder method of the SPWeb class.
You can use the GetFolder method of the SPWeb class to return a specified folder. Then you can access individual files in the folder. After instantiating an SPWeb object (for example, as oWebsite), use SPFolder oFolder = oWebsite.GetFolder("Shared Documents") (in Microsoft Visual Basic, use Dim oFolder As SPFolder = oWebsite.GetFolder("Shared Documents")) to return the Shared Documents folder for the site.
using (SPWeb oWebsite = new SPSite("http://Server/sites/SiteCollection").OpenWeb())
{
    string folderUrl = "/Shared Documents/MySubFolder";
    SPFolder oFolder = oWebsite.GetFolder(folderUrl);
    SPFileCollection collFile = oFolder.Files;

    foreach (SPFile oFile in collFile)
    {
        Label1.Text += "<BR>Url: " + oFile.Url.ToString() + " Size: " + oFile.Length.ToString();
    } 
}

Working with List Objects and Collections

To perform actions on list data in a SharePoint Web site, you must first obtain an SPWeb object to serve as an entry point to the object model, allowing you to access lists, items, documents, users, alerts, and so on. For information about how to return SharePoint Web sites, see Getting References to Sites, Web Applications, and Other Key Objects.

Most classes in the Microsoft.SharePoint and Microsoft.SharePoint.Administration namespaces start with SP. Generally, classes in the Microsoft.SharePoint assembly that do not start with this prefix represent Web form controls.
SharePoint Foundation typically does not save modifications of object properties to the database until you call the Update method on the specified object. The following example shows how to change the title and description for the Tasks list.


SPList oList = oWebsite.Lists["Tasks"];
oList.Title="New_Title";
oList.Description="List_Description";
oList.Update();

Monday, October 3, 2011

How to get the full URL from SPListItem in SharePoint

 
1.

static string GetItemUrl(SPListItem listItem)
{
return string.Format("{0}{1}?ID={2}", listItem.Web.Site.Url,
listItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl,listItem .ID);
}


2.

listItem.Web.Url  + the listItem.URL


3.

foreach (SPListItem listItemin list.Items)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(listItem.Xml);
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("z", "#RowsetSchema");
string fullURL = xmlDoc.SelectSingleNode("z:row", nsm).Attributes["ows_EncodedAbsUrl"].Value;
}