Social Icons

Pages

Monday, December 31, 2012

SharePoint Document Library C# - Retrieve Document Version Details


In this article, I am going show how to read the document version details from SharePoint document.

Create data table for version History


        void CreateVersionHistoryTable()
        {
            try
            {
                dtVersionCollection = new DataTable();
                dtVersionCollection.Columns.Add("VersionId", typeof(string));
                dtVersionCollection.Columns.Add("VersionLabel", typeof(string));
                dtVersionCollection.Columns.Add("CreatedBy", typeof(string));
                dtVersionCollection.Columns.Add("Created", typeof(string));
                dtVersionCollection.Columns.Add("ModifiedBy", typeof(string));
                dtVersionCollection.Columns.Add("Modified", typeof(string));
            }
            catch (Exception)
            {
                throw;
            }
        }


Read document version details

void GetAllVersion()
        {
            try
            {
                CreateVersionHistoryTable();
               SPSite Site = new SPSite(URL);
                using (SPWeb web = Site.OpenWeb())
                {
                    SPList list = web.Lists[DocLibraryName];
                    SPListItem item = list.GetItemById(DocumentID);
                    SPListItemVersionCollection  Fileversioncollection = item.Versions;
                    foreach (SPListItemVersion version in Fileversioncollection)
                    {
                        DataRow dr = dtVersionCollection.NewRow();
                        dr["VersionId"] = version.VersionId;
                        dr["Created"] = version.Created;
                        dr["Modified"] = version["Modified"];
                        dr["VersionLabel"] = version.VersionLabel;
                        try
                        {
                            SPFieldUserValue userValue = new SPFieldUserValue(web, version.CreatedBy.ToString());
                            SPUser userObject = userValue.User;
                            dr["CreatedBy"] = userObject;
                        }
                        catch (Exception)
                        {    }
                        try
                        {
                            SPFieldUserValue userValue = new SPFieldUserValue(web, version["Modified By"].ToString());
                            SPUser userObject = userValue.User;
                                dr["ModifiedBy"] = userObject;
                        }
                        catch (Exception)
                        { }
                        dtVersionCollection.Rows.Add(dr);
                    }
                }
            }
            catch (Exception)
            {
                 return;
            }
        }
 
 

Friday, December 21, 2012

SharePoint Document Library C# - Delete Document


Let’s see how we can delete document from SharePoint document library.
Here is the sample code;
private void DeleteDocument()
                {
                    try
                    {
                            DialogResult dr = MessageBox.Show(" Are you sure want to send the item to the site Recycle Bin?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dr == DialogResult.Yes)
                            {
                                this.Cursor = Cursors.WaitCursor;
                                int DocID = DocumentID;
                                string DocLib = DocLibrary Name;
                                SPSite Site = new SPSite(URL);
                                using (SPWeb web = Site.OpenWeb())
                                {
                                    SPList Selectedlist = web.Lists[DocLib];
                                    SPListItem item = Selectedlist.GetItemById(DocID);
                                    item.Recycle();
                                    MessageBox.Show("Document has successfully Recycled", "Message");
                                    this.Cursor = Cursors.Default;
                                }
                            }
                    }
                    catch (Exception ex)
                    {
                        this.Cursor = Cursors.Default;
                        MessageBox.Show(ex.Message, "Message");
                        return;
                    }
                }

Thursday, December 20, 2012

SharePoint Document Library C# - Upload/Download Document


In this article, I am going show how to Upload/Download document through code to a SharePoint document library. I am going to use SharePoint object model and refer the this A1, A2 articles for more information.Here is the sample code;

Upload Document

private void UploadDocument()
                {
                    try
                    {
                            this.Cursor = Cursors.WaitCursor;
                            string currentLibName = LibraryName;
                            string SelectedfilePath = FilePath;
                            bool overWrite = ISOverWrite;
                            SPSite Site = new SPSite(URL);
                            using (SPWeb web = Site.OpenWeb())
                            {
                                if (!System.IO.File.Exists(SelectedfilePath))
                                    throw new FileNotFoundException("File not found.", SelectedfilePath);
                                SPFolder libFolder = web.Folders[currentLibName];
                               
                                // Prepare to upload
                                string fileName = System.IO.Path.GetFileName(SelectedfilePath);
                                FileStream fileStream = File.OpenRead(SelectedfilePath);

                                //Check the existing File out if the Library Requires CheckOut
                                if (libFolder.RequiresCheckout)
                                {
                                    try
                                    {
                                        SPFile fileOld = libFolder.Files[fileName];
                                        fileOld.CheckOut();
                                    }
                                    catch { }
                                }
                               
                                // Upload document
                                SPFile spfile = libFolder.Files.Add(fileName, fileStream, overWrite);
                                libFolder.Update();
                                MessageBox.Show("File has successfully uploaded.", "Message");
                                try
                                {
                                    fileStream.Close();
                                }
                                catch (Exception) { }
                            }
                            this.Cursor = Cursors.Default;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Message");
                        return; ;
                    }


Download Document

void DownloadDocument()
                {
                    try
                    {
                            SPSite Site = new SPSite(URL);
                            string filePath = filePath;
                            string DocLib = LibraryName;
                            int docID = DocumentID;
                            using (SPWeb web = Site.OpenWeb())
                            {
                                SPList Selectedlist = web.Lists[DocLib];
                                SPListItem item = Selectedlist.GetItemById(docID);
                                byte[] binfile = item.File.OpenBinary();
                                FileStream fstream = new FileStream(filePath + "\\" + item.File.Name,
                                FileMode.Create, FileAccess.ReadWrite);
                                fstream.Write(binfile, 0, binfile.Length);
                                fstream.Close();
                                MessageBox.Show("Document has successfully saved.", "Message");
                            }

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Message");
                        return;
                    }

                }