Tuesday, October 8, 2013

SharePoint - Logging is not working - logs are empty

Almost every action that occurs in SharePoint is logged. SharePoint stores tons of data into its log file. This log files are very important because every time you get an error on your screen in SharePoint, you get a "Correlation id" on your screen. With this "Correlation id", you can search for detail error description in your logs and this is extremely helpful for resolving that error. SharePoint log files can be found in this path:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\LOGS

NOTE: Number "15" in path depends on the version of the SharePoint. If you have SharePoint 2007, then the number is 12. If you have SharePoint 2010, then the number is 14. If you have SharePoint 2013, then the number is 15.


But, sometimes you may ran into situation that logs are created in this folder, but they all have size of 0 byte (which means that nothing is being logged). Something made your logging service to stop.

 
Empty logs created in LOGS folder



There are three thing that you need to check:

1. Check the user permssions for your "SharePoint Tracing Service".

Go to: Start - Administrative Tools - Services - locate "SharePoint Tracing Service".
Make sure that user running this service is a member of Local Administrators group.

2. Restart service from step 1.

3. Free up some disk space.

Now, right  after the restart you may see that there is one log file created (small size, for example 1 KB) and others again have size of 0 byte. Now, open this new small sized log file and you will see the following error in it:

"Not enough free disk space available even if all inactive log files are deleted. The tracing service has temporarily stopped outputting trace messages to the log file. Tracing will resume when more than 1124 MB of disk space becomes available."


It is clear now that you don't have enough free disk space and this is the reason why your logging stopped working in the first place. Make more than 1 GB of free space and your logger will start logging immediately.

Tuesday, October 1, 2013

SharePoint - Create Top Navigation menu programatically

In my previous post, I wrote about creating Quick Launch navigation programmatically . In this post, I will show how to create Top Navigation programmatically. It is easy since the code is almost the same as for Quick Launch.

Top Navigation is navigation located on the top of the screen and it is important part of almost every SharePoint site (marked in red square on the image). 

SharePoint Top Navigation


The code is very simple, it consists of reading the Top Navigation object and deleting the existing links in it. And then, after the existing Top Navigation is cleared, putting the new links which we have read from some text file.

C# CODE:



SPSecurity.RunWithElevatedPrivileges(delegate()
{
     using (SPSite oSiteCollection = new SPSite("http://mySiteUrl"))
     {
           using (SPWeb oWeb = oSiteCollection.OpenWeb())
           {
                 // Create the node.
                 SPNavigationNodeCollection _topNav = oWeb.Navigation.TopNavigationBar;
                 // Delete all existing items in Top Navigation
                 int _counter = 0;
                 while (_topNav.Count != _counter && _topNav.Count > 0)
                 {
                       _counter++;
                       SPNavigationNode _item = _topNav[0];
                       _item.Delete();
                 }

                 // Here we read links that will go in Top Navigation from text file
                 // Let's say that in "abc.txt" file we have links written as (title|link):
                 //
                 // My project |/sites/MyTestSite1
                 // Project documents|/sites/MyDocLib2
                 // Reports|/sites/MyCustomReports
                 //...
   
                 string _line;

                 System.IO.StreamReader _file = new System.IO.StreamReader("abc.txt");
                 while ((_line = _file.ReadLine()) != null)
                 {
                       string[] _splitStr = _line.Split('|'); 
                       string _title = _splitStr[0];
                       string _url = _splitStr[1];

                       SPNavigationNode _SPNode = new SPNavigationNode(_title, _url, true);
 
                       _topNav.AddAsLast(_SPNode);
                 }
                 _file.Close();
           }
     }
});