Translate

Saturday, October 13, 2012

Using SPFolder SharePoint Object


Using SPFolder SharePoint Object

SPFolder is one of the SharePoint object and it is a folder in the SharePoint list.To access 
an SPFolder object, first we need to retrieve an SPListItem object and use
the Folder property of that "SPListItem" which will return an SPFolder if the list item is a folder,
or else it  will returns null. 


Following are some of the properties which SPFolder exposed.

S.No.
Property Name
Description
1.
Name
Name of the folder
2.
Url
This return the site-relative URL of the folder
3.
Files
This returns an SPFileCollection object that contains SPFile
objects representing all the files found in the folder
4.
Item
Returns the SPListItem object that corresponds to the folder
5.
SubFolders
This returns an SPFolderCollection object that contains
SPFolder objects for all the subfolders contained within the
folder
6.
Exists
This returns a bool value that indicates whether the folder
exists
7.
Delete()
This method that deletes the folder
8.
CopyTo(string)
It Copies the folder and its contents to the URL specified by
the string parameter
9.
MoveTo(string)
Moves the folder and its contents to the URL specified by
the string parameter
10.
ItemCount
The count of children of this folder including
subfolders
11.
ServerRelativeUrl
This return the server-relative URL for the folder


The following WebPart  shows how to access the SPFolder object using SharePoint Object Model.



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;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;


namespace SPFolderDemo
{

 [ToolboxItemAttribute(false)]
    public class SPFolerSample : WebPart
    {

      StringBuilder spFolderSb = new StringBuilder();
        Literal SpLiteral = new Literal();
     


 protected override void CreateChildControls()
        {

            try
            {
SPList list=null
using(SPWeb web=SPContext.Current.Web)
{
list=web.List["SharedDocuments"];
}
SPListItem item=list.item[0];//for testing pointing to index "0"
SPFolder folder=item.Folder;
spFolderSb.Append("Folder Details");
spFolderSb.Append("Folder url:");
spFolderSb.Append(folder.Url);
spFolderSb.Append("Folder contains file");
spFolderSb.Append(folder.Files.Count);
spFolderSb.Append("Is Folder contains sub folder");
spFolderSb.Append(folder.SubFolder.Co);

SpLiterall.Text = spFolderSb.ToString();
this.Controls.Add(SpLiteral);


            }
            catch (Exception ex)
            {
                String.Format("Exception. ", ex);
            }

       }
    }









No comments:

Post a Comment