January 21, 2014

Reduce growing disk space by reducing WSS_LOGGING dbase size

WSS logging database grows very fast and it cause the storage problem most of the time in sharepoint server 2010.
To reduce the size of the logging database or to purge the old data from the logging database we can perform these steps.
Execute the following script:
By Default the system retains 14 days of Usage data in this database. By following PowerShell command we can check and change the value of retention days of this database.
Add-PsSnapin Microsoft.SharePoint.PowerShell
Write-Host "Data retention policy, which is set to 14 days by default.Going to set it to 3 days."

   1:  Add-PsSnapin Microsoft.SharePoint.PowerShell
   2:  Write-Host "Data retention policy, which is set to 14 days by default.Going to set it to 3 days."
   3:  Get-SPUsageDefinition
   4:  $defs = Get-SPUsageDefinition
   6:  Foreach($def in $defs)
   7:  {
   8:  Set-SPUsageDefinition –Identity $def.Name –DaysRetained 3
   9:  } 
write-host "Now, open Central Admin and choose, Monitoring > Configure usage and health data collection > Log Collection Schedule>."
Write-host "Execute the two Timer jobs:"
Write-host "1)Microsoft SharePoint Foundation Usage Data Import"
Write-host "2)Microsoft SharePoint Foundation Usage Data Processing"
Write-host "The SQL logging database will now contain some free space"
Write-host "which your SQL DBA can ‘free-up’ within SQL Management Studio "
write-host "or running the ‘DBCC ShrinkFile’ T-SQL command."
Remove-PsSnapin Microsoft.SharePoint.PowerShell
After that we need to run the two timer jobs to clean the old data 'Microsoft SharePoint Foundation Usage Data Import' and 'Microsoft SharePoint Foundation Usage Data Processing'.
Go to Sharepoint Central Administration -> Monitoring -> Configure Usage and health data collection-> Log Collection Schedule.
And it will take you to the timer jobs.

Now Click on both the Job Definitions one by one and hit 'Run Now' to run the timer jobs

Once the timer jobs is run you can check and confirm database has released the space.
You can shrink the database after this by using 'DBCC ShrinkFile’ T-SQL command

If you have any questions you can reach out our SharePoint Consulting team here.

All CQWP web part appears as ErrorWebPart when fetching using object model in Console Application

In console application, while looping through all web parts in a page, if a page contains ContentByQueryWebpart, exception those web part will be rendered as ErrorWebParts
Because of the following code is called by the specific properties of the ContentByQueryWebpart
(actually its parent, the CmsDataFormWebpart):

   1:  internal static string MakeServerRelativeUrl(string url)
   2:  {
   3:      return concatenateUrls(SPContext.GetContext(HttpContext.Current).Site.ServerRelativeUrl, url);
   4:  }
The webpart will always call the SPContext, but from a console application there is no web-context. Therefore when initiating the
ContentByQueryWebpart, it will always thow an exception like:
"An error occured while setting the value of this property: Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart:MainXslLink - Exception has been thrown by the target of an invocation."
Workaround for this would be to provide it with a context.

   1:  if (HttpContext.Current == null)
   2:              {
   3:           isContextNull = true;
   4:                  HttpRequest request = new HttpRequest("", myweb.Url, "");
   5:                  HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
   6:                  HttpContext.Current.Items["HttpHandlerSPWeb"] = myweb;
   7:               }

If you have any questions you can reach out our SharePoint Consulting team here.

Get Site Column Id in SharePoint using UI

Many a times we need site column id to be used somewhere or the other and for that we might require to get it reasonably quickly and more over without having to write any code to fetch it.
Navigate to Site Settings of SharePoint Site and Select “Site Content Types” from the Galleries group. It will show the list of content types.
Select the content type which contains site column. It will display the settings page for the content type as below
Select the column for which need to get the ID. It will take to the column settings page as shown below
Notice the URL of this page in browser, which will be similar to below

Notice that the Fid at the end of the URL is your site column Id
Important note:
In browser URL, this string may be encoded, so its display like following :

Explanation of the encoding:
%7B = “{“
%2D = “-“
%7D = “}”

If you have any questions you can reach out our SharePoint Consulting team here.