Tuesday, August 19, 2014

Write text log entry inside 14 Hive logs from SharePoint Timer job

This blog will demonstrate, how to create/write log file in text format inside 14\Hive folder. Idea behind this is to register error in text format inside 14\Hive folder. Lets consider this problem statement : Get list of Organization Profiles where value of property "OrganizationMotto" is null or Empty. "OrganizationMotto" is custom property, created through UI. To create custom property follow this steps. 1. Go to User profile application 2. Under Organizations --> Click Manage Organizations Properties 3. From Top bar click "New Property" 4.Specify Name and Display Name. Name cannot be changed but display name can be. So make sure you giving correct name. 5.Specify Type and length. 6.Hit Save. There are other settings you can do while create property. Such as replicating property to user information list , showing in profile page etc. After saving you can see custom property in Manage Organizations properties section. Now you need to create SharePoint , which will be deployed to the share point webapplication level. Below article showcase step by step guide to create timer job. Kindly review that if audience is new to SharePoint. http://dotnetfinder.wordpress.com/2010/07/24/creatingcustomsharepointtimerjob2010/ Now after creating timer job, you can find Execute method which will contain core body for logic. Below is the code reference public override void Execute(Guid targetInstanceId) { SPWebApplication webApplication = this.Parent as SPWebApplication; using (var rootSite = new SPSite(webApplication.Sites[0].Url)) { // Get reference for Service Context SPServiceContext serviceContext = SPServiceContext.GetContext(spSite); //Get reference of OrganizationProfileManager which contains all Organization Profile objects OrganizationProfileManager orgProfileManager = new OrganizationProfileManager(serviceContext); //Now we want to create file under 14\Hive, so this path can be easily retrieved using GetGenericSteupPath method of SPSUtility. This will get path Program files\Common Files\Microsoft Shared\Web Service Extensions\14\Logs string logsPath = SPUtility.GetGenericSetupPath("Logs"); string filepath = logsPath + @"\OrganizationProfileLog.txt"; // Get object of StreamWriter to create and write file StreamWriter streamWriter = File.CreateText(filepath); string orgDisplayName = string.Empty; //iterate each object of OrganizationProfile to check OrganizationMotto value. foreach (OrganizationProfile orgProfile in orgProfileManager) { try { orgDisplayName = orgProfile.DisplayName; if (orgProfile["OrganizationMotto"].Value == null) { //WriteLine method will write string to the new line. streamWriter.WriteLine("OrganizationMotto property value of "+ orgDisplayName +" is empty"); } else { if (!string.IsNullOrEmpty(orgProfile["OrganizationMotto"].Value.ToString())) { streamWriter.WriteLine("OrganizationMotto property value of " + orgDisplayName + " is empty"); } } } catch (Exception exception) { streamWriter.WriteLine(exception.Message); } } streamWriter.Flush(); streamWriter.Close(); } } After adding this code. Deploy timer job and execute it. Then go to 14\Logs folder and search for OrganizationProfileLog.txt file. Open this file to see if there is any issue registered .

No comments:

Post a Comment

Google+ Followers