SharePoint 2010: How to Set Default Calculated Value to DateTime Column by Code
SharePoint 2010 מאפשר להגדיר תאריך ברירת מחדל ספציפי לשדה DateTime באמצעות קוד.
לדוגמא (הקוד הבא יגרום לכך שתאריך ברירת המחדל בשדה DateTime יהיו 17/5/2012, כיוון שהקוד "רץ" בתאריך זה לצורך הדוגמא):
using (SPSite site = new SPSite(http://siteurl/))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("ListName");
if (list != null)
{
SPField fld = list.Fields["FieldName"];
fld.DefaultFormula = "=DATEVALUE(\"" +
DateTime.Today.ToShortDateString() + "\")";
fld.Update();
}
}
}
והתוצאה תהיה:
