[{"data":1,"prerenderedAt":403},["ShallowReactive",2],{"article-output-text-to-log-file":3},{"article":4,"tags":44,"previous":59,"next":229},{"id":5,"title":6,"author":7,"body":8,"createdAt":33,"description":34,"extension":35,"img":34,"meta":36,"navigation":37,"path":38,"seo":39,"stem":40,"tags":41,"updatedAt":33,"__hash__":43},"articles\u002Farticles\u002Foutput-text-to-log-file.md","Output text to log file","[object Object]",{"type":9,"value":10,"toc":29},"minimark",[11,19,26],[12,13,14,15,18],"p",{},"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.",[16,17],"br",{},"\n ",[20,21,25],"pre",{"className":22},[23,24],"brush:","csharp","  \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,27,28],{},"Cool eh?",{"title":30,"searchDepth":31,"depth":31,"links":32},"",2,[],"2015-04-20T08:07:14.3500000-04:00",null,"md",{},true,"\u002Farticles\u002Foutput-text-to-log-file",{"title":6,"description":34},"articles\u002Foutput-text-to-log-file",[42],"aspnet","-p4EpWiQEkv4r2hKPqYV9kpN3R2ct3mifyOTeovi3gM",[45],{"id":46,"title":47,"body":48,"description":52,"extension":35,"img":53,"meta":54,"name":42,"navigation":37,"path":55,"seo":56,"stem":57,"__hash__":58},"tags\u002Ftags\u002Faspnet.md","Aspnet",{"type":9,"value":49,"toc":50},[],{"title":30,"searchDepth":31,"depth":31,"links":51},[],"ASP.NET is an open source web framework, created by Microsoft, for building modern web apps and services with .NET.","https:\u002F\u002Fimages.unsplash.com\u002Fphoto-1598313183973-4effcded8d5e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=675&q=80",{},"\u002Ftags\u002Faspnet",{"description":52},"tags\u002Faspnet","SlUGLdZWQy8mYOWC6OetgJkwVulWNURoVHeuESIDleI",{"id":60,"title":61,"author":7,"body":62,"createdAt":220,"description":221,"extension":35,"img":34,"meta":222,"navigation":37,"path":223,"seo":224,"stem":225,"tags":226,"updatedAt":220,"__hash__":228},"articles\u002Farticles\u002Ffeature-eventreceiver-get-web-context.md","Feature.EventReceiver Get Web Context",{"type":9,"value":63,"toc":218},[64,106],[12,65,66,67,69,70,72,73,75,76,78,79,18,81,83,89,90,93,94,97,98,101,102,105],{},"In order to get the current SPWeb context that the feature was activated on I have used the following extension method.  Regardless if the feature is web or site scoped the following GetWeb extension will return the current web context.",[16,68],{},"\nTypically, you would do something like the following.  This works fine if the feature is scoped for web.  If however it is scoped for Site then it will no longer work (.Parent becomes SPSite)",[16,71],{},"\nSPWeb site = (SPWeb)properties.Feature.Parent; ",[16,74],{},"\nUsing the following extension you can now use something like the following.",[16,77],{},"\nSPWeb web = properties.GetWeb();  \u002F\u002Fmuch cleaner",[16,80],{},[16,82],{},[84,85,88],"span",{"className":86},[87],"kwrd","using"," System;\n",[84,91,88],{"className":92},[87]," System.Collections.Generic;\n",[84,95,88],{"className":96},[87]," System.Linq;\n",[84,99,88],{"className":100},[87]," System.Text;\n",[84,103,88],{"className":104},[87]," Microsoft.SharePoint;",[12,107,108,112,113,112,117,121,122,127,131,135,139,143,112,146,149,150,160,161,165,166,170,171,175,176,112,180,170,183,186,187,190,191,112,195,199,200,205,206,210,211,213,214,18,216,18],{},[84,109,111],{"className":110},[87],"public"," ",[84,114,116],{"className":115},[87],"static",[84,118,120],{"className":119},[87],"class"," Extensions\n{\n",[84,123,126],{"className":124},[125],"rem","\u002F\u002F\u002F \u003Csummary>",[84,128,130],{"className":129},[125],"\u002F\u002F\u002F Gets the web.",[84,132,134],{"className":133},[125],"\u002F\u002F\u002F \u003C\u002Fsummary>",[84,136,138],{"className":137},[125],"\u002F\u002F\u002F \u003Cparam name=\"properties\">The properties.\u003C\u002Fparam>",[84,140,142],{"className":141},[125],"\u002F\u002F\u002F \u003Creturns>\u003C\u002Freturns>",[84,144,111],{"className":145},[87],[84,147,116],{"className":148},[87]," SPWeb\n",[84,151,153,154],{"className":152},[125],"\u002F\u002F",[155,156,157],"a",{"href":157,"rel":158},"http:\u002F\u002Fblog.mattsmith.co.nz\u002FLists\u002FPosts\u002FPost.aspx?List=c7bdac80-1d4e-4732-9e67-cefde9c03d31&amp;ID=51",[159],"nofollow","\nGetWeb(",[84,162,164],{"className":163},[87],"this"," SPFeatureReceiverProperties properties){\n    SPWeb site;\n    ",[84,167,169],{"className":168},[87],"if"," (properties.Feature.Parent ",[84,172,174],{"className":173},[87],"is"," SPWeb) {\n        site = (SPWeb)properties.Feature.Parent;\n    } ",[84,177,179],{"className":178},[87],"else",[84,181,169],{"className":182},[87],[84,184,174],{"className":185},[87]," SPSite) {\n        site = ((SPSite)properties.Feature.Parent).RootWeb;\n    } ",[84,188,179],{"className":189},[87]," {\n        ",[84,192,194],{"className":193},[87],"throw",[84,196,198],{"className":197},[87],"new"," Exception(",[84,201,204],{"className":202},[203],"str","\"Unable to retrieve SPWeb - this feature is not Site or Web-scoped.\"",");\n    }\n    ",[84,207,209],{"className":208},[87],"return"," site;\n    }\n}",[16,212],{},"\nCool eh?",[16,215],{},[16,217],{},{"title":30,"searchDepth":31,"depth":31,"links":219},[],"2015-04-20T08:07:14.5400000-04:00","In order to get the current SPWeb context that the feature was activated...",{},"\u002Farticles\u002Ffeature-eventreceiver-get-web-context",{"title":61,"description":221},"articles\u002Ffeature-eventreceiver-get-web-context",[227],"sharepoint","0dvtWcX_WjOm6yWA2aUZbBzFdRBtbbC3K1kZCZu1pz4",{"id":230,"title":231,"author":7,"body":232,"createdAt":395,"description":396,"extension":35,"img":34,"meta":397,"navigation":37,"path":398,"seo":399,"stem":400,"tags":401,"updatedAt":395,"__hash__":402},"articles\u002Farticles\u002Fsharepoint-method-return-splist-or-create-a-new-one.md","SharePoint Method–Return SPList (or Create a new one)",{"type":9,"value":233,"toc":393},[234,237,387,389],[12,235,236],{},"Method that returns an existing SPList on a SharePoint web (based on list name) or create a new one if not found.",[20,238,241],{"className":239,"code":240,"language":24,"meta":30,"style":30},"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",[242,243,244,251,256,262,268,274,280,286,292,298,304,310,316,322,328,334,340,346,352,357,363,369,375,381],"code",{"__ignoreMap":30},[84,245,248],{"class":246,"line":247},"line",1,[84,249,250],{},"public static SPList EnsureList(SPWeb site, string listName, SPListTemplateType template, bool onQuickLaunch) {  \n",[84,252,253],{"class":246,"line":31},[84,254,255],{},"      \n",[84,257,259],{"class":246,"line":258},3,[84,260,261],{},"    SPList list = null;  \n",[84,263,265],{"class":246,"line":264},4,[84,266,267],{},"    Guid listID = Guid.Empty;  \n",[84,269,271],{"class":246,"line":270},5,[84,272,273],{},"    if (site != null) {  \n",[84,275,277],{"class":246,"line":276},6,[84,278,279],{},"        foreach (SPList item in site.Lists) {  \n",[84,281,283],{"class":246,"line":282},7,[84,284,285],{},"            if (item.Title.ToLower() == listName.ToLower()) {  \n",[84,287,289],{"class":246,"line":288},8,[84,290,291],{},"                list = item;  \n",[84,293,295],{"class":246,"line":294},9,[84,296,297],{},"                listID = item.ID;  \n",[84,299,301],{"class":246,"line":300},10,[84,302,303],{},"                break;  \n",[84,305,307],{"class":246,"line":306},11,[84,308,309],{},"            }  \n",[84,311,313],{"class":246,"line":312},12,[84,314,315],{},"        }  \n",[84,317,319],{"class":246,"line":318},13,[84,320,321],{},"  \n",[84,323,325],{"class":246,"line":324},14,[84,326,327],{},"        if (list == null) {  \n",[84,329,331],{"class":246,"line":330},15,[84,332,333],{},"            listID = site.Lists.Add(listName,\"\", template);  \n",[84,335,337],{"class":246,"line":336},16,[84,338,339],{},"            list = site.Lists[listID];  \n",[84,341,343],{"class":246,"line":342},17,[84,344,345],{},"            list.OnQuickLaunch = onQuickLaunch;  \n",[84,347,349],{"class":246,"line":348},18,[84,350,351],{},"            list.Update();              \n",[84,353,355],{"class":246,"line":354},19,[84,356,315],{},[84,358,360],{"class":246,"line":359},20,[84,361,362],{},"    } else {  \n",[84,364,366],{"class":246,"line":365},21,[84,367,368],{},"        throw new Exception(\"In EnsureSiteDataList SPWeb is null\");  \n",[84,370,372],{"class":246,"line":371},22,[84,373,374],{},"    }  \n",[84,376,378],{"class":246,"line":377},23,[84,379,380],{},"    return list;  \n",[84,382,384],{"class":246,"line":383},24,[84,385,386],{},"}\n",[12,388,28],{},[390,391,392],"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":30,"searchDepth":31,"depth":31,"links":394},[],"2015-04-20T08:07:14.2600000-04:00","Method that returns an existing SPList on a SharePoint web",{},"\u002Farticles\u002Fsharepoint-method-return-splist-or-create-a-new-one",{"title":231,"description":396},"articles\u002Fsharepoint-method-return-splist-or-create-a-new-one",[227],"23Ps1y5WrMGsI6PnpJjW-M6_FbRRv2pr0lErNFzPytw",1781574762325]