Friday, November 11, 2011

Update Theme for All Site collections, Sub-Sites in SharePoint

Requirement: Update new theme in for All Site collections, Sub-Sites in SharePoint

C# code to do that:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace Theme_Update
{
    class Theme_Update
    {
        static void Main(string[] args)
        {
            string site;
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                }

                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //Enumerate through each site
                foreach (SPSite tmpSite in tmpRootColl)
                {
                    //Enumerate through each web for the site

                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                            //Apply the default theme for the current Web
                            tmpWeb.AllowUnsafeUpdates = true;
                            tmpWeb.ApplyTheme("none");
                            tmpWeb.Update();
                            tmpWeb.ApplyTheme("Custom-Theme");
                            tmpWeb.Update();
                            tmpWeb.AllowUnsafeUpdates = false;

                        //Log to a file, where the theme is applied!
                            StreamWriter SW;
                            SW = File.AppendText("E:\\ThemeLog.txt");
                            SW.WriteLine(tmpWeb.Url);
                            SW.Close();

                            //Dispose of the Web Object
                            tmpWeb.Dispose();
                        }
                    //Dispose of the Site Object
                    tmpSite.Dispose();
                }
                //Dispose of the Root Site Object
                tmpRoot.Dispose();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Theme Updater", ex.Message);
            }
        }
    }

}


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Tuesday, November 8, 2011

Archive (Backup and Delete) IIS Logs in SharePoint

IIS logs are the requests from clients, and written into text-based log files. It captures every web page/image/script requests. By default, IIS logs stored in C:\ of WFE servers. If SharePoint is being accessed a lot, It may cause disk space problem for your OS drive so quickly. So we can change this log path. Here is how:
  1. Go into IIS Manager for each WFE. (Start > run > Inetmgr)
  2. select the server name in the Connections panel on the left.
  3. In the Features view shown in the middle panel, select Logging.
  4. For the log file, change the directory to a more suitable drive and folder. For example, I’ve changed the path for the log files to D:\Logs\IIS.
sharepoint 2010 iis logs location

How about backing up them Monthly and deleting old logs?
Its a good idea to backup and clean IIS Logs every Month. These Logs are highly compressible up to 500%!!! (Meaning 500 MB file to 5MB file).

Steps:
1. Download and import the PowerShell community Extension (for write-zip cmd-let)
2. Write the script to Zip and Delete IIS Logs
3. Schedule the script to run on every month ( or based on your schedule)

Step 1: Download and import the PowerShell community Extension
Download the PowerShell community Extension from http://pscx.codeplex.com/, and then unblock it(Right click the downloaded .zip file > go to Properties > click on "Unblock" button)

Fire up PowerShell, Get in to your modules folder, typically it would be:C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
cd $PSHome\Modules

Extract the PowerShell community Extension Zip file to a folder, Say: pscx . Now, Import the downloaded modules:
import-module Pscx

Done!

Step 2: Write the script to Zip and Delete IIS Logs
Now the next step: Writing PowerShell script to Zip and delete the Old log files, Here is the script:

setExecutionPolicy RemoteSigned #important
$date=get-date -UFormat %m-%d-%y
write-zip -Path C:\Logs\IIS -Outputpath C:\Logs\$date.zip
move-item c:\Logs\$date.zip D:\IISLogBackup\
get-childitem C:\Logs\IIS -include *.log -recurse | remove-item
Place the above script in to a file, Say: d:\Scripts\IISCleanup.ps1

Step 3: Schedule the script to run on every month
Finally, We need to schedule the script in Windows Task scheduler. Go to Start > All Programs > Administrative tools > Task Scheduler, and create the task to run the script at d:\Scripts\IISCleanup.ps1.
powershell.exe D:\scripts\IISLogsClear.ps1 If you get exceptions like "File not found.." then add the paths D:\Scripts, PowerShell's path in System Environment variable.

Update: Related to this, there is a codeplex utility to  compress and move SharePoint Logs (Not IIS Logs): http://dlc.codeplex.com/


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


You might also like:

Related Posts Plugin for WordPress, Blogger...