Social Icons

Pages

Monday, January 7, 2013

SharePoint Document Library C# - Delete all the document versions using OM


In this article, I am going show how to delete all the document versions details from SharePoint document library.


private void DeleteAllVersion()
        {
            try
            {
                    DialogResult dr = MessageBox.Show("Are you sure you want to send all provious versions associated with this file to the site Recycle Bin?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        SPSite Site = new SPSite(requestUrl);
                        using (SPWeb web = Site.OpenWeb())
                        {
                            SPList list = web.Lists[DocLibrary];
                            SPListItem item = list.GetItemById(DocumentID);
                            SPListItemVersionCollection Fileversioncollection = item.Versions;
                            Fileversioncollection.RecycleAll();
                            MessageBox.Show("Document versions have been successfully deleted.", "Message");
                        }
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message");
                return;

            }
        }
 

Saturday, January 5, 2013

SharePoint Document Library C# - Delete particular version document using OM


Let’s see, How to delete particular document version from SharePoint document library.

        private void DeleteVersion()
        {
            try
            {
                    DialogResult dr = MessageBox.Show("Are you sure tou want to send this version to the site Recycle bin?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        SPSite Site = new SPSite(requestUrl);
                        using (SPWeb web = Site.OpenWeb())
                        {
                            SPList list = web.Lists[DocLibrary];
                            SPListItem item = list.GetItemById(DocID);
                            SPListItemVersionCollection Fileversioncollection = item.Versions;
                            SPListItemVersion version = Fileversioncollection.GetVersionFromID(selectedversionID);
                            version.Recycle();
                            MessageBox.Show("Document version has been successfully deleted.", "Message");
                        }
                    }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message");
                return;
            }
        }

Thursday, January 3, 2013

SharePoint Document Library C# - Restore particular version document


Let’s see, How to restore particular version document in SharePoint document library.

private void RestoreDocument()
        {
            try
            {
                    DialogResult dr = MessageBox.Show("you are about to replace the current version with the selected version?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        SPSite Site = new SPSite(URL);
                        using (SPWeb web = Site.OpenWeb())
                        {
                            SPList list = web.Lists[DocLibrary];
                            SPListItem item = list.GetItemById(DocID);
                            item.Versions.RestoreByID(selectedversionID);
                            MessageBox.Show("Document version has been successfully restored.", "Message");
                        }
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message");
                return;
            }
        }