This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Web.UI.WebControls.WebParts; | |
using System.Xml.Serialization; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.WebControls; | |
using Microsoft.SharePoint.WebPartPages; | |
namespace FSDocsXREFFilter | |
{ | |
[Guid("497b816f-628c-4bdb-bca0-395d011a9572")] | |
public class XREFWebPart : System.Web.UI.WebControls.WebParts.WebPart | |
{ | |
Label lblTitle; | |
TextBox txtSearch; | |
Button btnFilter; | |
string strDefaultView = string.Empty; | |
public XREFWebPart() | |
{ | |
} | |
protected override void CreateChildControls() | |
{ | |
string strURL = Page.Request.Url.ToString(); | |
strDefaultView = strURL.Substring(strURL.LastIndexOf('/') + 1); | |
base.CreateChildControls(); | |
lblTitle = new Label(); | |
txtSearch = new TextBox(); | |
btnFilter = new Button(); | |
btnFilter.Text = "Search"; | |
lblTitle.Text = "XREF Documents"; | |
btnFilter.Click += new EventHandler(btnFilterClick); | |
this.Controls.Add(lblTitle); | |
this.Controls.Add(new LiteralControl(" ")); | |
this.Controls.Add(txtSearch); | |
this.Controls.Add(new LiteralControl(" ")); | |
this.Controls.Add(btnFilter); | |
string oquery = ""; | |
try | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = true; | |
if (txtSearch.Text.Trim() != "") | |
{ | |
oquery = ""; | |
oquery += "" + txtSearch.Text.Trim() + ""; | |
oquery += ""; | |
} | |
string strDocLibName = "Approved Documents"; | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
using (SPSite site = new SPSite(SPContext.Current.Web.Url)) | |
{ | |
using (SPWeb web = site.OpenWeb()) | |
{ | |
web.AllowUnsafeUpdates = true; | |
string s = SPContext.Current.Web.Url; | |
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)web.Lists[strDocLibName]; | |
SPViewCollection viewColl = oDocumentLibrary.Views; | |
viewColl.List.BreakRoleInheritance(true); | |
} | |
} | |
}); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
finally | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = false; | |
txtSearch.Text = ""; | |
} | |
} | |
protected void btnFilterClick(object sender, EventArgs e) | |
{ | |
if (txtSearch.Text.Trim() == "") | |
{ | |
try | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = true; | |
string strDocLibName = "Approved Documents"; | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
using (SPSite site = new SPSite(SPContext.Current.Web.Url)) | |
{ | |
using (SPWeb web = site.OpenWeb()) | |
{ | |
web.AllowUnsafeUpdates = true; | |
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)web.Lists[strDocLibName]; | |
SPViewCollection viewColl = oDocumentLibrary.Views; | |
viewColl.List.BreakRoleInheritance(true); | |
Page.Response.Redirect(strDefaultView, false); | |
} | |
} | |
}); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
finally | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = false; | |
txtSearch.Text = ""; | |
} | |
} | |
else | |
{ | |
string strViewName = "XREFDocuments"; | |
string oquery = ""; | |
try | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = true; | |
if (txtSearch.Text.Trim() != "") | |
{ | |
oquery = ""; | |
oquery += "" + txtSearch.Text.Trim() + ""; | |
oquery += ""; | |
} | |
string strDocLibName = "Approved Documents"; | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
using (SPSite site = new SPSite(SPContext.Current.Web.Url)) | |
{ | |
using (SPWeb web = site.OpenWeb()) | |
{ | |
web.AllowUnsafeUpdates = true; | |
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)web.Lists[strDocLibName]; | |
SPViewCollection viewColl = oDocumentLibrary.Views; | |
viewColl.List.BreakRoleInheritance(true); | |
int intCount = 0; | |
for (int i = 0; i < viewColl.Count; i++) | |
{ | |
Microsoft.SharePoint.SPView view = viewColl[i]; | |
if (viewColl[i].ToString() == "XREFDocuments") | |
{ | |
intCount = 1; | |
} | |
} | |
if (intCount == 0) | |
{ | |
System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection(); | |
collViewFields.Add("XREF_x0020_Documents"); | |
collViewFields.Add("Admin_x0020_Comments"); | |
viewColl.Add(strViewName, collViewFields, oquery, 100, true, false); | |
} | |
for (int i = 0; i < viewColl.Count; i++) | |
{ | |
Microsoft.SharePoint.SPView view = viewColl[i]; | |
if (viewColl[i].ToString() == "XREFDocuments") | |
{ | |
view.Query = oquery; | |
view.Update(); | |
} | |
} | |
} | |
} | |
}); | |
Page.Response.Redirect(strViewName + ".aspx", false); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
finally | |
{ | |
SPContext.Current.Web.AllowUnsafeUpdates = false; | |
txtSearch.Text = ""; | |
} | |
} | |
} | |
} | |
} |