Search This Blog

2009-04-30

SPDocumentLibrary

What is SPDocumentLibrary /What are the methods and properties of SPDocumentLibrary /How can we use SPDocumentLibrary

SPDocumentLibrary represents the sharepoint document library lists.

Properties:-

CheckedOutFiles
Gets the collection of files that are checked out of the document library.

Methods:-

GetItemsInFolder
Returns a collection of items from the document library based on the specified view and folder.

Note:- All other properties are inherited from SPList.


Some Code Snippets:-

//*******code snippet which shows the uploading of document
SPSite site = new SPSite("http://gpchbs-s6137:92");
SPWeb web = site.OpenWeb();
SPDocumentLibrary docLib1=null;
SPDocumentLibrary docLib2=null;
if ((web.Lists["Audio"].BaseType == SPBaseType.DocumentLibrary) && (web.Lists["Awards"].BaseType == SPBaseType.DocumentLibrary))
{
docLib1 = (SPDocumentLibrary)web.Lists["Audio"];

docLib2 = (SPDocumentLibrary)web.Lists["Awards"];
}
if (docLib1 != null && docLib2 != null)
{
docLib1.ContentTypesEnabled = true;
SPFolder folder1 = docLib1.RootFolder.SubFolders.Add("FirstChild");
folder1.Item["Title"]= "FirstFolder1";
folder1.Item.Update();
folder1.SubFolders.Add("FirstSF");
folder1.Update();
docLib1.Update();

//Create the stream object to add a file
FileStream fstream = new FileStream("C:\\KMLists.txt", FileMode.Open, FileAccess.Read);
SPFile file1= folder1.Files.Add("KMLists.txt", fstream);
folder1.Update();
fstream.Close();

file1.Item["Title"] = "KMDetails";
file1.Item.Update();
file1.Update();

folder1.CopyTo(docLib2.RootFolder.Url + "/" + folder1.Name+"_Copy");
docLib2.Update();

foreach (SPFolder folder in docLib2.RootFolder.SubFolders)
{
Console.WriteLine("Folder Names at root level :" + folder.Name);
Console.WriteLine(folder.ServerRelativeUrl);
Console.WriteLine(folder.Url);
Console.WriteLine(folder.SubFolders.Count);

foreach (SPFile file in folder.Files)
{
Console.WriteLine("File length is :" + file.Length.ToString());
Console.WriteLine(file.ServerRelativeUrl);
Console.WriteLine(file.Url);
Console.WriteLine(file.Name);

}

Console.ReadLine();
}




}





//********which shows downloading a document and displaying the versions
SPSite site = new SPSite("http://gpchbs-s6137:92");
SPWeb web = site.OpenWeb();
SPFile file1 = web.GetFile("/Audio/FirstChild/KMLists.txt");
if (file1 != null)
{
byte[] barr = file1.OpenBinary();
FileStream fstream = new FileStream("C:\\Arijit\\PracticeDemos\\backupkmlist.txt", FileMode.CreateNew, FileAccess.Write);
fstream.Write(barr, 0, Convert.ToInt32(file1.Length));
fstream.Close();

FileStream fstream2 = new FileStream("C:\\screenshot.doc", FileMode.Open, FileAccess.Read);
file1.SaveBinary(fstream2);
file1.Update();
fstream2.Close();



foreach (SPFileVersion ver in file1.Versions)
{
Console.WriteLine(ver.CheckInComment);
Console.WriteLine(ver.CreatedBy.LoginName);
Console.WriteLine(ver.ID.ToString());
Console.WriteLine(ver.VersionLabel);
Console.WriteLine(ver.Url);
Console.WriteLine(ver.Level.ToString());
}

Console.ReadLine();

}

1 comment:

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards