[{"data":1,"prerenderedAt":606},["ShallowReactive",2],{"article-feature-eventreceiver-get-web-context":3},{"article":4,"tags":184,"previous":199,"next":577},{"id":5,"title":6,"author":7,"body":8,"createdAt":172,"description":173,"extension":174,"img":175,"meta":176,"navigation":177,"path":178,"seo":179,"stem":180,"tags":181,"updatedAt":172,"__hash__":183},"articles\u002Farticles\u002Ffeature-eventreceiver-get-web-context.md","Feature.EventReceiver Get Web Context","[object Object]",{"type":9,"value":10,"toc":168},"minimark",[11,56],[12,13,14,15,18,19,21,22,24,25,27,28,30,31,33,39,40,43,44,47,48,51,52,55],"p",{},"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,17],"br",{},"\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,20],{},"\nSPWeb site = (SPWeb)properties.Feature.Parent; ",[16,23],{},"\nUsing the following extension you can now use something like the following.",[16,26],{},"\nSPWeb web = properties.GetWeb();  \u002F\u002Fmuch cleaner",[16,29],{},"\n ",[16,32],{},[34,35,38],"span",{"className":36},[37],"kwrd","using"," System;\n",[34,41,38],{"className":42},[37]," System.Collections.Generic;\n",[34,45,38],{"className":46},[37]," System.Linq;\n",[34,49,38],{"className":50},[37]," System.Text;\n",[34,53,38],{"className":54},[37]," Microsoft.SharePoint;",[12,57,58,62,63,62,67,71,72,77,81,85,89,93,62,96,99,100,110,111,115,116,120,121,125,126,62,130,120,133,136,137,140,141,62,145,149,150,155,156,160,161,163,164,30,166,30],{},[34,59,61],{"className":60},[37],"public"," ",[34,64,66],{"className":65},[37],"static",[34,68,70],{"className":69},[37],"class"," Extensions\n{\n",[34,73,76],{"className":74},[75],"rem","\u002F\u002F\u002F \u003Csummary>",[34,78,80],{"className":79},[75],"\u002F\u002F\u002F Gets the web.",[34,82,84],{"className":83},[75],"\u002F\u002F\u002F \u003C\u002Fsummary>",[34,86,88],{"className":87},[75],"\u002F\u002F\u002F \u003Cparam name=\"properties\">The properties.\u003C\u002Fparam>",[34,90,92],{"className":91},[75],"\u002F\u002F\u002F \u003Creturns>\u003C\u002Freturns>",[34,94,61],{"className":95},[37],[34,97,66],{"className":98},[37]," SPWeb\n",[34,101,103,104],{"className":102},[75],"\u002F\u002F",[105,106,107],"a",{"href":107,"rel":108},"http:\u002F\u002Fblog.mattsmith.co.nz\u002FLists\u002FPosts\u002FPost.aspx?List=c7bdac80-1d4e-4732-9e67-cefde9c03d31&amp;ID=51",[109],"nofollow","\nGetWeb(",[34,112,114],{"className":113},[37],"this"," SPFeatureReceiverProperties properties){\n    SPWeb site;\n    ",[34,117,119],{"className":118},[37],"if"," (properties.Feature.Parent ",[34,122,124],{"className":123},[37],"is"," SPWeb) {\n        site = (SPWeb)properties.Feature.Parent;\n    } ",[34,127,129],{"className":128},[37],"else",[34,131,119],{"className":132},[37],[34,134,124],{"className":135},[37]," SPSite) {\n        site = ((SPSite)properties.Feature.Parent).RootWeb;\n    } ",[34,138,129],{"className":139},[37]," {\n        ",[34,142,144],{"className":143},[37],"throw",[34,146,148],{"className":147},[37],"new"," Exception(",[34,151,154],{"className":152},[153],"str","\"Unable to retrieve SPWeb - this feature is not Site or Web-scoped.\"",");\n    }\n    ",[34,157,159],{"className":158},[37],"return"," site;\n    }\n}",[16,162],{},"\nCool eh?",[16,165],{},[16,167],{},{"title":169,"searchDepth":170,"depth":170,"links":171},"",2,[],"2015-04-20T08:07:14.5400000-04:00","In order to get the current SPWeb context that the feature was activated...","md",null,{},true,"\u002Farticles\u002Ffeature-eventreceiver-get-web-context",{"title":6,"description":173},"articles\u002Ffeature-eventreceiver-get-web-context",[182],"sharepoint","0dvtWcX_WjOm6yWA2aUZbBzFdRBtbbC3K1kZCZu1pz4",[185],{"id":186,"title":187,"body":188,"description":192,"extension":174,"img":193,"meta":194,"name":182,"navigation":177,"path":195,"seo":196,"stem":197,"__hash__":198},"tags\u002Ftags\u002Fsharepoint.md","Sharepoint",{"type":9,"value":189,"toc":190},[],{"title":169,"searchDepth":170,"depth":170,"links":191},[],"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":192},"tags\u002Fsharepoint","1yMA-SLjOHmy0x6_m2U0v9b58OVV2Jg1i0vhgXavZ7M",{"id":200,"title":201,"author":7,"body":202,"createdAt":568,"description":569,"extension":174,"img":565,"meta":570,"navigation":177,"path":571,"seo":572,"stem":573,"tags":574,"updatedAt":568,"__hash__":576},"articles\u002Farticles\u002Fdesign-guidelines-classes-vs-structures.md","Design Guidelines– Classes vs. Structures",{"type":9,"value":203,"toc":566},[204,218,328,343,348,350,379,497,503,532,535,557,561],[12,205,206,207,209,213,214,217],{},"My goal is to cover some .NET 4 Platform features through a series of ‘guideline’ type entries demonstrating some new and existing functionality.  In this first entry, I have seen both classes and structures used throughout applications.  The following is meant to describe and explain good design considerations.",[16,208],{},[210,211,212],"strong",{},"Structure"," types are well suited for modeling mathematical, geometrical, and other \"atomic\" entities in your application. A ",[210,215,216],{},"structure"," (like an enumeration) is a user-defined type; however, structures are not simply a collection of name\u002Fvalue pairs. Rather, structures are types that can contain any number of data fields and members that operate on these fields.",[219,220,223,224,223],"div",{"id":221,"style":222},"codeSnippetWrapper","text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;","  \n",[219,225,223,228,223,223,237,223,223,245,223,223,253,223,223,260,223,223,263,223,223,268,223,223,276,223,223,279,223,223,282,223,223,284,223,223,289,223,223,296,223,223,299,223,223,301,223,223,303,223,223,308,223,223,315,223,223,323,223,223,325,223],{"id":226,"style":227},"codeSnippet","text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[229,230,232,236],"pre",{"style":231},"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[34,233,235],{"style":234},"color: #0000ff;","struct"," Point {",[229,238,240,241],{"style":239},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;","    ",[34,242,244],{"style":243},"color: #008000;","\u002F\u002F Fields of the structure.",[229,246,240,247,62,249,252],{"style":231},[34,248,61],{"style":234},[34,250,251],{"style":234},"int"," X;",[229,254,240,255,62,257,259],{"style":239},[34,256,61],{"style":234},[34,258,251],{"style":234}," Y;",[229,261,262],{"style":231}," ",[229,264,240,265],{"style":239},[34,266,267],{"style":243},"\u002F\u002F Add 1 to the (X, Y) position.",[229,269,240,270,62,272,275],{"style":231},[34,271,61],{"style":234},[34,273,274],{"style":234},"void"," Increment() {",[229,277,278],{"style":239},"        X++; Y++;",[229,280,281],{"style":231},"    }",[229,283,262],{"style":239},[229,285,240,286],{"style":231},[34,287,288],{"style":243},"\u002F\u002F Subtract 1 from the (X, Y) position.",[229,290,240,291,62,293,295],{"style":239},[34,292,61],{"style":234},[34,294,274],{"style":234}," Decrement() {",[229,297,298],{"style":231},"        X--; Y--;",[229,300,281],{"style":239},[229,302,262],{"style":231},[229,304,240,305],{"style":239},[34,306,307],{"style":243},"\u002F\u002F Display the current position.",[229,309,240,310,62,312,314],{"style":231},[34,311,61],{"style":234},[34,313,274],{"style":234}," Display() {",[229,316,317,318,322],{"style":239},"        Console.WriteLine(",[34,319,321],{"style":320},"color: #006080;","\"X = {0}, Y = {1}\"",", X, Y);",[229,324,281],{"style":231},[229,326,327],{"style":239},"}",[12,329,330,331,333,334,336,337,339,340,342],{},"myPoint.X=100;\nmyPoint.Y=76;\nmyPoint.Display();",[16,332],{},"\nYou can create a structure using new keyword which will use the structures’ default constructor.",[16,335],{},"\ni.e. Point myPoint = new Point(); \u002F\u002F each field will be automatically set to its default value",[16,338],{},"\nA custom constructor can also be created with a structure which allows you to specify the values of a field upon creation.",[16,341],{},"\ni.e. Public Point(int xpos, int ypos)\nPoint p2 = new Point(50,50);",[12,344,345],{},[210,346,347],{},"Class",[210,349],{},[12,351,352,354,355,359,360,363,364,366,367,369,372,373,375,378],{},[210,353],{},"A class is a user-defined type that is composed of field data (often called ",[356,357,358],"em",{},"member variables",") and members that operate on this data (such as constructors, properties, methods, events, and so forth). Collectively, the set of field data represents the \"state\" of a class instance (otherwise known as an ",[356,361,362],{},"object","). The power of object-based languages such as C# is that by grouping data and related functionality in a unified class definition, you are able to model your software after entities in the real world.",[16,365],{},"\nI am going to jump to some less known aspects of a class.",[16,368],{},[356,370,371],{},"Default constructor",": By definition, a default constructor never takes arguments.  After allocating the new object into memory, the default constructor ensures that all field data of the class is set to an appropriate default value.\nCustom constructor:Allows the user with a simple and consistent way to initialize the state of an object directly at the time of creation.  Note however that as soon as you define a custom constructor the compiler removes the default constructor.  Thereby if you wish to continue to have both a default and custom constructors you must explicitly declare the default constructor.",[16,374],{},[356,376,377],{},"Chaining constructors ","utilizes the ‘this’ keyword.  Understand that using the this keyword to chain constructor calls is never mandatory.  Notice in the example below however that the constructor Public Motorcycle(string name) takes one string argument but continues to call the additional constructor Public Motorcyle(int intensity, string name) via the this keyword i.e. this(0,name)",[219,380,223,381,223],{"id":221,"style":222},[219,382,223,383,223,223,388,223,223,391,223,223,399,223,223,407,223,223,409,223,223,414,223,223,419,223,223,427,223,223,437,223,223,439,223,223,446,223,223,451,223,223,453,223,223,458,223,223,467,223,223,470,223,223,475,223,223,478,223,223,481,223,223,483,223,223,486,223,223,489,223,223,492,223,223,495,223],{"id":226,"style":227},[229,384,385,387],{"style":231},[34,386,70],{"style":234}," Motorcycle",[229,389,390],{"style":239},"{",[229,392,393,394,62,396,398],{"style":231},"  ",[34,395,61],{"style":234},[34,397,251],{"style":234}," driverIntensity;",[229,400,393,401,62,403,406],{"style":239},[34,402,61],{"style":234},[34,404,405],{"style":234},"string"," driverName;",[229,408,262],{"style":231},[229,410,393,411],{"style":239},[34,412,413],{"style":243},"\u002F\u002F Constructor chaining.",[229,415,393,416,418],{"style":231},[34,417,61],{"style":234}," Motorcycle() {}",[229,420,393,421,423,424,426],{"style":239},[34,422,61],{"style":234}," Motorcycle(",[34,425,251],{"style":234}," intensity)",[229,428,429,430,432,433,436],{"style":231},"    : ",[34,431,114],{"style":234},"(intensity, ",[34,434,435],{"style":320},"\"\"",") {}",[229,438,262],{"style":239},[229,440,393,441,423,443,445],{"style":231},[34,442,61],{"style":234},[34,444,405],{"style":234}," name)",[229,447,429,448,450],{"style":239},[34,449,114],{"style":234},"(0, name) {}",[229,452,262],{"style":231},[229,454,393,455],{"style":239},[34,456,457],{"style":243},"\u002F\u002F This is the 'master' constructor that does all the real work.",[229,459,393,460,423,462,464,465,445],{"style":231},[34,461,61],{"style":234},[34,463,251],{"style":234}," intensity, ",[34,466,405],{"style":234},[229,468,469],{"style":239},"  {",[229,471,240,472,474],{"style":231},[34,473,119],{"style":234}," (intensity > 10)",[229,476,477],{"style":239},"    {",[229,479,480],{"style":231},"      intensity = 10;",[229,482,281],{"style":239},[229,484,485],{"style":231},"    driverIntensity = intensity;",[229,487,488],{"style":239},"    driverName = name;",[229,490,491],{"style":231},"  }",[229,493,494],{"style":239},"...",[229,496,327],{"style":231},[12,498,499,502],{},[356,500,501],{},"Optional Arguments"," allow you to define supplied default values to the incoming arguments.  i.e. with the constructor below the user can create a new RocketShip with zero, one or two arguments.  This is a .NET 4 feature only.\nRocketShip = new RocketShip();\nRocketShip = new RocketShip(name:=”TestShip”);\nRocketShip = new RocketShip(10); \u002F\u002Fsets speed = 10, leaving name =  ””",[219,504,223,505,223],{"id":221,"style":222},[219,506,223,507,223,223,512,223,223,526,223,223,529,223,327],{"id":226,"style":227},[229,508,509],{"style":231},[34,510,511],{"style":243},"\u002F\u002F Single constructor using optional args.",[229,513,514,516,517,519,520,522,523,525],{"style":239},[34,515,61],{"style":234}," RocketShip(",[34,518,251],{"style":234}," speed = 0, ",[34,521,405],{"style":234}," name = ",[34,524,435],{"style":320},"){",[229,527,528],{"style":239},"  driverSpeed = speed;",[229,530,531],{"style":231},"  driverName = name;",[210,533,534],{},"Classes vs. Structures Guidelines",[536,537,538,542,545,548,551,554],"ul",{},[539,540,541],"li",{},"Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.",[539,543,544],{},"Do not define a structure unless the type has all the following characteristics:",[539,546,547],{},"It logically represents a single value, similar to primitive types (integer, double, and so on).",[539,549,550],{},"It has an instance size smaller than 16 bytes.",[539,552,553],{},"It is immutable.",[539,555,556],{},"It will not have to be boxed frequently.",[558,559,560],"blockquote",{},"  \nIf one or more of these conditions are not met, create a reference type instead of a structure. Failure to adhere to this guideline can negatively impact performance.  \n",[562,563],"img",{"style":564,"src":565,"alt":169},"display: none;","\u002Farticles\u002Fimages\u002Fstructure2.jpg",{"title":169,"searchDepth":170,"depth":170,"links":567},[],"2015-04-20T08:07:14.6400000-04:00","My goal is to cover some .NET 4 Platform features through a series of &lsquo;guideline&rsquo; type entries demonstrating some new and existing functionality.",{},"\u002Farticles\u002Fdesign-guidelines-classes-vs-structures",{"title":201,"description":569},"articles\u002Fdesign-guidelines-classes-vs-structures",[575],"netcore","-N3gPMr8ZsjnQA0jdKCMNLjbID8lUwbxN-6kUVEFlhA",{"id":578,"title":579,"author":7,"body":580,"createdAt":598,"description":175,"extension":174,"img":175,"meta":599,"navigation":177,"path":600,"seo":601,"stem":602,"tags":603,"updatedAt":598,"__hash__":605},"articles\u002Farticles\u002Foutput-text-to-log-file.md","Output text to log file",{"type":9,"value":581,"toc":596},[582,587,593],[12,583,584,585,30],{},"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,586],{},[229,588,592],{"className":589},[590,591],"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,594,595],{},"Cool eh?",{"title":169,"searchDepth":170,"depth":170,"links":597},[],"2015-04-20T08:07:14.3500000-04:00",{},"\u002Farticles\u002Foutput-text-to-log-file",{"title":579,"description":175},"articles\u002Foutput-text-to-log-file",[604],"aspnet","-p4EpWiQEkv4r2hKPqYV9kpN3R2ct3mifyOTeovi3gM",1781574766675]