AfterProperties kräver InternalName
AfterProperties kommer inte leda till Exception, men de kommer returnera bara null, om du använder DisplayName. Man måste ha InternalName. Här är ett litet exempel på hur man kan få ut värden före och efter uppdateringen. Exemplet har testats i ItemUpdated-eventreceiver.
var before = properties.BeforeProperties;
var after = properties.AfterProperties;
var contentDisplayName = "News Content";
var list = properties.List;
var contentInternalName = list.Fields\[contentDisplayName\].InternalName;
var contentBefore = before\[contentInternalName\];
var contentAfter = after\[contentInternalName\];
window.onload
Sharepoint ger en annan lösning för window.onload och jQuery(document).ready(function() {}); _spBodyOnLoadFunctionNames.push(‘gerdaRibbonInit’);"
ViewState
Om du vill behålla dina värden på variabler mellan postbacks, använd ViewState:
if (ViewState\["MaxNumberOfArticles"\] != null)
{
MaxNumberOfArticles = (int) ViewState\["MaxNumberOfArticles"\];
}
MaxNumberOfArticles += 10;
ViewState.Add("MaxNumberOfArticles", MaxNumberOfArticles);
RenderNews();
Webpart Livscykel
http://nishantrana.wordpress.com/2009/02/14/understanding-web-part-life-cycle/ OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part. LoadViewState – The view state of the web part is populated over here. CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it. OnLoad User Generated Event – for e.g. button click on the web part. OnPreRender – Here we can change any of the web part properties before the control output is drawn. RenderContents – Html Output is generated. SaveViewState - View state of the web part is serialized and saved. Dispose UnLoad.
Kontrollera om SPView finns
bool exists = splist.Views.Cast().Any(view => string.Equals(view.Title, viewName));
Lägg till en sida
Här är ett litet exempel på hur man kan lägga till sidor:
private void PublishNews(SPWeb web, string spamnewsname)
{
PublishingSite pSite = new PublishingSite(web.Site);
SPContentType ctype = pSite.ContentTypes\["NewsPageContentType"\];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
PageLayout pageLayout = pageLayouts\["/\_catalogs/masterpage/MyNewsLayout.aspx"\];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
PublishingPage pPage = pPages.Add(spamnewsname, pageLayout);
SPListItem newpage = pPage.ListItem;
newpage\["Title"\] = "Page added programmatically";
newpage\["NewsContent"\] = lorem;
newpage.Update();
newpage.File.CheckIn("all looks good");
newpage.File.Publish("all looks good");
}
formatera int
Här finns en lista över formateringsmöjligheter med C# string.Format
String.Format("{0:00000}", 15); // "00015"
resharper
Bara att rekommendera. Dagens: Ctrl-Shift-Backspace: Go to last edit. http://www.jetbrains.com/resharper/webhelp/Reference__Keyboard_Shortcuts.html http://blog.drorhelper.com/2009/05/15-resharper-keyboard-shortcuts-you.html