Wednesday, September 17, 2014

Quick Application Logger Code

//todo: check anonymous (elevated privileges?)
public class Log
{
     private const string LogSource = "mYsOURCE";
     public static void WriteEntry(Exception Event)
     {
           string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
                 if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, Event.ToString());
                 }
                 catch { }
           });
     }


     public static void WriteEntry(string msg)
     {          

           string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {                

                 if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {                    

                      EventLog.WriteEntry(sSource, msg);
                 }                

                 catch { }
           });
     }
 
    

    
     public static void WriteErrorEntry(Exception Event)
     {
 
 
         string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
 
 
               if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, Event.ToString(), EventLogEntryType.Error);
 
 
              }
 
 
               catch { }
           });
     }
 
 


     public static void WriteErrorEntry(string msg)
     {
 
 
         string sSource = LogSource;
           string sLog = "Application";
           SPSecurity.RunWithElevatedPrivileges(() =>
           {
 
 
               if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                 try
                 {
                      EventLog.WriteEntry(sSource, msg, EventLogEntryType.Error);
 
 
              }
 
 
               catch { }
           });
     }
 
 

}
 

Monday, September 8, 2014

how to create taxonomyField with genesis

        [SPGENField(
            ID = "{BADCE114-FB99-4F27-A777-030E336D1EFA}",
            StaticName = "MMDSubject",
            InternalName = "MMDSubject",
            DisplayName = "$Resources:Model,Field_MMDSubject_DisplayName;",
            CustomType = "TaxonomyFieldType",
            Group = "$Resources:Model,FieldGroup_Title;")]
        public class MMDSubject : SPGENField<MMDSubject, TaxonomyField, TaxonomyFieldValue>
        {
            protected override void OnProvisionFinalized(TaxonomyField field, SPFieldCollection fieldCollection, bool isParentList, bool updatedOnly)
            {
                SPSite site = fieldCollection.Web.Site;
                SPWeb rootWeb = site.RootWeb;

                TaxonomySession session = new TaxonomySession(site);
                TermStore termStore = session.TermStores["MMD"];

                Group group_TaxTest = termStore.Groups["Root"];
                TermSet ts_TaxTest = group_cbsTaxTest.TermSets["נושאים"];

                field.SspId = termStore.Id;
                field.TermSetId = ts_TaxTest.Id;
                field.TargetTemplate = string.Empty;
                //cbsTaxTest.AllowMultipleValues = true;
                field.CreateValuesInEditForm = true;
                field.Open = true;
                //cbsTaxTest.IsAnchorValid = true;
                field.AnchorId = Guid.Empty;
                field.Update();
                base.OnProvisionFinalized(field, fieldCollection, isParentList, updatedOnly);
            }
        }

if you need it as a multi dont forget to use multi class: TaxonomyFieldValueCollection