Skip to content

Instantly share code, notes, and snippets.

@ElliotWood
Created March 28, 2013 02:12
Show Gist options
  • Save ElliotWood/5259955 to your computer and use it in GitHub Desktop.
Save ElliotWood/5259955 to your computer and use it in GitHub Desktop.
SharePoint Content Query Webpart - Auto CommonViewFields
public class ContentDisplayWebPart : ContentByQueryWebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string headerlink = "/Style%20Library/XSL%20Style%20Sheets/CustomXsl/______.xsl";
string itemlink = "/Style%20Library/XSL%20Style%20Sheets/CustomXsl/______.xsl";
if (this.HeaderXslLink != headerlink) { this.HeaderXslLink = headerlink; }
if (this.ItemXslLink != itemlink) { this.ItemXslLink = itemlink; }
if (this.CommonViewFields != (string)HttpContext.Current.Cache[this.Title + "_CommonViewFields"])
{
Dictionary<string, string> CommonViewFieldsDictionary = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(this.ListGuid) && this.ListGuid != Guid.Empty.ToString())
{
SPWeb web = SPContext.Current.Site.OpenWeb(this.WebUrl.Replace("~sitecollection", ""));
foreach (SPContentType ContentType in web.Lists[this.ListName].ContentTypes)
{
foreach (SPField field in ContentType.Fields)
{
if (!CommonViewFieldsDictionary.ContainsKey(field.InternalName))
{
CommonViewFieldsDictionary.Add(field.InternalName, field.TypeAsString);
}
}
}
foreach (SPField field in web.Lists[this.ListName].Fields)
{
if (!CommonViewFieldsDictionary.ContainsKey(field.InternalName))
{
CommonViewFieldsDictionary.Add(field.InternalName, field.TypeAsString);
}
}
CommonViewFieldsDictionary.ToList().ForEach(item =>
{
CommonViewFields += string.Format("{0},{1};", item.Key, item.Value);
});
}
HttpContext.Current.Cache.Add(this.Title + "_CommonViewFields", CommonViewFields, null, DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment