[{"data":1,"prerenderedAt":294},["ShallowReactive",2],{"article-sharepoint-method-return-splist-or-create-a-new-one":3},{"article":4,"tags":191,"previous":206,"next":235},{"id":5,"title":6,"author":7,"body":8,"createdAt":179,"description":180,"extension":181,"img":182,"meta":183,"navigation":184,"path":185,"seo":186,"stem":187,"tags":188,"updatedAt":179,"__hash__":190},"articles\u002Farticles\u002Fsharepoint-method-return-splist-or-create-a-new-one.md","SharePoint Method–Return SPList (or Create a new one)","[object Object]",{"type":9,"value":10,"toc":177},"minimark",[11,15,170,173],[12,13,14],"p",{},"Method that returns an existing SPList on a SharePoint web (based on list name) or create a new one if not found.",[16,17,22],"pre",{"className":18,"code":19,"language":20,"meta":21,"style":21},"language-csharp shiki shiki-themes github-light github-dark","public static SPList EnsureList(SPWeb site, string listName, SPListTemplateType template, bool onQuickLaunch) {  \n      \n    SPList list = null;  \n    Guid listID = Guid.Empty;  \n    if (site != null) {  \n        foreach (SPList item in site.Lists) {  \n            if (item.Title.ToLower() == listName.ToLower()) {  \n                list = item;  \n                listID = item.ID;  \n                break;  \n            }  \n        }  \n  \n        if (list == null) {  \n            listID = site.Lists.Add(listName,\"\", template);  \n            list = site.Lists[listID];  \n            list.OnQuickLaunch = onQuickLaunch;  \n            list.Update();              \n        }  \n    } else {  \n        throw new Exception(\"In EnsureSiteDataList SPWeb is null\");  \n    }  \n    return list;  \n}  \n","csharp","",[23,24,25,33,39,45,51,57,63,69,75,81,87,93,99,105,111,117,123,129,135,140,146,152,158,164],"code",{"__ignoreMap":21},[26,27,30],"span",{"class":28,"line":29},"line",1,[26,31,32],{},"public static SPList EnsureList(SPWeb site, string listName, SPListTemplateType template, bool onQuickLaunch) {  \n",[26,34,36],{"class":28,"line":35},2,[26,37,38],{},"      \n",[26,40,42],{"class":28,"line":41},3,[26,43,44],{},"    SPList list = null;  \n",[26,46,48],{"class":28,"line":47},4,[26,49,50],{},"    Guid listID = Guid.Empty;  \n",[26,52,54],{"class":28,"line":53},5,[26,55,56],{},"    if (site != null) {  \n",[26,58,60],{"class":28,"line":59},6,[26,61,62],{},"        foreach (SPList item in site.Lists) {  \n",[26,64,66],{"class":28,"line":65},7,[26,67,68],{},"            if (item.Title.ToLower() == listName.ToLower()) {  \n",[26,70,72],{"class":28,"line":71},8,[26,73,74],{},"                list = item;  \n",[26,76,78],{"class":28,"line":77},9,[26,79,80],{},"                listID = item.ID;  \n",[26,82,84],{"class":28,"line":83},10,[26,85,86],{},"                break;  \n",[26,88,90],{"class":28,"line":89},11,[26,91,92],{},"            }  \n",[26,94,96],{"class":28,"line":95},12,[26,97,98],{},"        }  \n",[26,100,102],{"class":28,"line":101},13,[26,103,104],{},"  \n",[26,106,108],{"class":28,"line":107},14,[26,109,110],{},"        if (list == null) {  \n",[26,112,114],{"class":28,"line":113},15,[26,115,116],{},"            listID = site.Lists.Add(listName,\"\", template);  \n",[26,118,120],{"class":28,"line":119},16,[26,121,122],{},"            list = site.Lists[listID];  \n",[26,124,126],{"class":28,"line":125},17,[26,127,128],{},"            list.OnQuickLaunch = onQuickLaunch;  \n",[26,130,132],{"class":28,"line":131},18,[26,133,134],{},"            list.Update();              \n",[26,136,138],{"class":28,"line":137},19,[26,139,98],{},[26,141,143],{"class":28,"line":142},20,[26,144,145],{},"    } else {  \n",[26,147,149],{"class":28,"line":148},21,[26,150,151],{},"        throw new Exception(\"In EnsureSiteDataList SPWeb is null\");  \n",[26,153,155],{"class":28,"line":154},22,[26,156,157],{},"    }  \n",[26,159,161],{"class":28,"line":160},23,[26,162,163],{},"    return list;  \n",[26,165,167],{"class":28,"line":166},24,[26,168,169],{},"}\n",[12,171,172],{},"Cool eh?",[174,175,176],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":21,"searchDepth":35,"depth":35,"links":178},[],"2015-04-20T08:07:14.2600000-04:00","Method that returns an existing SPList on a SharePoint web","md",null,{},true,"\u002Farticles\u002Fsharepoint-method-return-splist-or-create-a-new-one",{"title":6,"description":180},"articles\u002Fsharepoint-method-return-splist-or-create-a-new-one",[189],"sharepoint","23Ps1y5WrMGsI6PnpJjW-M6_FbRRv2pr0lErNFzPytw",[192],{"id":193,"title":194,"body":195,"description":199,"extension":181,"img":200,"meta":201,"name":189,"navigation":184,"path":202,"seo":203,"stem":204,"__hash__":205},"tags\u002Ftags\u002Fsharepoint.md","Sharepoint",{"type":9,"value":196,"toc":197},[],{"title":21,"searchDepth":35,"depth":35,"links":198},[],"Microsoft SharePoint","https:\u002F\u002Fimages.unsplash.com\u002Fphoto-1598313183973-4effcded8d5e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=675&q=80",{},"\u002Ftags\u002Fsharepoint",{"description":199},"tags\u002Fsharepoint","1yMA-SLjOHmy0x6_m2U0v9b58OVV2Jg1i0vhgXavZ7M",{"id":207,"title":208,"author":7,"body":209,"createdAt":227,"description":182,"extension":181,"img":182,"meta":228,"navigation":184,"path":229,"seo":230,"stem":231,"tags":232,"updatedAt":227,"__hash__":234},"articles\u002Farticles\u002Foutput-text-to-log-file.md","Output text to log file",{"type":9,"value":210,"toc":225},[211,218,223],[12,212,213,214,217],{},"The following method can be used save string messages out to log files.  In the code below I am using a couple class fields to establish the log file name.  These can be customized to meet your need.",[215,216],"br",{},"\n ",[16,219,222],{"className":220},[221,20],"brush:","  \npublic void Output(string message) {  \n        if (_logFilePath != stringEmpty) {  \n        string fileName = \"LogFile\";  \n        if (_contentDatabaseName != string.Empty)  \n            fileName += \"_\" + _contentDatabaseName;  \n  \n        fileName += \".txt\";  \n        string filePath = System.IO.Path.Combine(_logFilePath, fileName);  \n        System.IO.StreamWriter sw = System.IO.File.AppendText(filePath); \u002F\u002F Change filename  \n        try {  \n            string logLine = System.String.Format(\"{0:G}: {1}.\", System.DateTime.Now, message);  \n            sw.WriteLine(logLine);  \n        } finally {  \n            sw.Close();  \n        }  \n    }  \n}",[12,224,172],{},{"title":21,"searchDepth":35,"depth":35,"links":226},[],"2015-04-20T08:07:14.3500000-04:00",{},"\u002Farticles\u002Foutput-text-to-log-file",{"title":208,"description":182},"articles\u002Foutput-text-to-log-file",[233],"aspnet","-p4EpWiQEkv4r2hKPqYV9kpN3R2ct3mifyOTeovi3gM",{"id":236,"title":237,"author":7,"body":238,"createdAt":287,"description":182,"extension":181,"img":182,"meta":288,"navigation":184,"path":289,"seo":290,"stem":291,"tags":292,"updatedAt":287,"__hash__":293},"articles\u002Farticles\u002Fiterate-over-object-properties-and-property-attributes-w-reflection.md","Iterate Over Object Properties and Property Attributes w\u002FReflection",{"type":9,"value":239,"toc":285},[240,243,275,281],[12,241,242],{},"Define custom attribute class as follows as well as placing the attribute reference on the property “FirstName”.  In the following we are creating our own custom attribute by defining an attribute class which derives from Attribute which makes identifying attribute definitions in metadata easy.  The AttributeUsage attribute can be used to limit which asset the attribute can be placed such as class, struct, property etc.  In addition, in the example below I have disallowed multiple similar attributes from being used on the same property designated by AllowMultiple = false     ",[16,244,246,247,250,252,253,261,272],{"className":245},[221,20],"    ",[26,248,249],{},"AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)",[215,251],{},"\npublic class CustomItemAttribute : Attribute {",[16,254,259],{"className":255,"code":257,"language":258},[256],"language-text","    public string FieldName { get; set; }  \n\n    private bool _isMultiValue = false;  \n    public bool IsMultiValue {  \n        get { return _isMultiValue; }  \n        set {  \n            _isMultiValue = value;  \n        }  \n    }  \n\n    public CustomItemAttribute(string fieldName) {  \n        this.FieldName = fieldName;          \n    }  \n}  \n","text",[23,260,257],{"__ignoreMap":21},[12,262,263,264,266,269,271],{},"public class Item{",[215,265],{},[26,267,268],{},"CustomItem(\"myfieldname\")",[215,270],{},"\npublic string FirstName { get; set;}",[12,273,274],{},"}",[12,276,277,278,280],{},"Now to iterate over the properties. Notice in the class Item above how we can exclude the 'Attribute' text in the name of the attribute applied to the FirstName property. Below we can use Type.GetProperties method to get the names of the properties for a specific type. The method GetProperties returns an array of PropertyInfo objects and the property names aer available through PropertyInfo.Name. If you want to get only a subset of the properties such as public static ones you can use BindingFlags parameters (Public\u002FNonPublic, Instance\u002FStatic). i.e. PropertyInfo",[26,279],{}," infos = typeof(Item).GetProperties(BindingFlags.Public|BindingFlags.Static);",[16,282,284],{"className":283},[221,20],"        private void IterateOverProperties() {  \n            CustomItemAttribute customItemAttribute;  \n            Type type = typeof(Item);  \n        \u002F\u002Ffor each property of object of Item  \n            foreach (PropertyInfo propInfo in type.GetProperties()) {  \n                \u002F\u002Ffor each custom attribute on the property loop  \n                foreach CustomItemAttribute attr in propInfo.GetCustomAttributes(typeof(CustomItemAttribute), false)) {  \n                    customItemAttribute = attr as CustomItemAttribute;  \n                    if (customItemAttribute != null) {  \n                        string propertyName = propInfo.Name;  \n                        string fieldName = customItemAttribute.FieldName;              \n            \u002F\u002FTODO: add your logic here  \n                    }   \n                }  \n            }  \n        }",{"title":21,"searchDepth":35,"depth":35,"links":286},[],"2015-04-20T08:07:14.1700000-04:00",{},"\u002Farticles\u002Fiterate-over-object-properties-and-property-attributes-w-reflection",{"title":237,"description":182},"articles\u002Fiterate-over-object-properties-and-property-attributes-w-reflection",[233],"ZthYNR0xRiDSvIjg9fVGjlPJjgpoE9X9J0htudQFbIM",1781574766725]