Thursday, 10 March 2016

Search the CheckedOut Files in the Document Library using Server Object Model in SharePoint

private void CreateToolbar()
{
ToolBarButton toolbuttonNew = (ToolBarButton)Page.LoadControl(ServiceDevelopmentConstants.CREATE_TOOLBARBUTTON);
toolbuttonNew.Text = this.newButton;
ToolBarButton toolbuttonExport = (ToolBarButton)Page.LoadControl(ServiceDevelopmentConstants.CREATE_TOOLBARBUTTON);
toolbuttonExport.Text = this.newExport;
if (!GetCheckoutLangguageDetails())
{
toolbuttonNew.Click += new EventHandler(this.ToolbuttonNewClick);
toolbuttonExport.Click += new EventHandler(this.ToolbuttonExportClick);
}
else
{
toolbuttonExport.ToolTip = “Files are Checked Out”;
toolbuttonNew.ToolTip = “”;
}
ToolBar toolbar = (ToolBar)Page.LoadControl(ServiceDevelopmentConstants.CREATE_TOOLBAR);
toolbar.Buttons.Controls.Add(toolbuttonNew);
toolbar.Buttons.Controls.Add(toolbuttonExport);
Controls.Add(toolbar);
}
private bool GetCheckoutLangguageDetails()
{
bool isCheckOut = false;
const string listUrl = “/CheckOutTokens/Forms/AllItems.aspx”;
List<string> FileNames = new List<string>(); //file names to check
FileNames.Add(“Translation1033.xml”);
FileNames.Add(“Translation1044.xml”);
FileNames.Add(“Translation2068.xml”);
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = SPContext.Current.Web)
foreach (SPWeb webs in site.AllWebs)
{
webs.AllowUnsafeUpdates = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string URL = webs.ServerRelativeUrl;
SPList results = webs.Lists.Cast<SPList>().FirstOrDefault(item => Equals(string.Compare(item.DefaultViewUrl, URL + listUrl, true), 0));
if (results != null)
{
SPDocumentLibrary docLibrary = (SPDocumentLibrary)webs.Lists[results.ID];
foreach (string FileName in FileNames)
{
SPListItem listItemsearch = docLibrary.Items.Cast<SPListItem>().FirstOrDefault(item => Equals(string.Compare(item.Name, FileName, true), 0)); //Gets the list whole url of the site
if (listItemsearch != null)
{
SPListItem listItem = docLibrary.Items[listItemsearch.UniqueId];//List ID
SPFile file = listItem.File;
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
isCheckOut = true;
}
}
}
}
});
web.AllowUnsafeUpdates = false;
}
}
return isCheckOut;
}

No comments:

Post a Comment