Pages

Wednesday, 28 August 2013

Powershell script to iterate through all Site Collections and Sub-sites for a Web Application

The Powershell script below iterates through all Site Collections and Sub-sites for a Web Application . It can be used for Running a Script on All Sites in a Web-Application




Add-PSSnapin Microsoft.Sharepoint.Powershell
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
$webApplicationURL = "http://sedwdevrtm/"
$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)
{
  Write-Host "Web Application : " + $webApp.Name

  foreach($siteColl in $webApp.Sites)
    {
     if($siteColl -ne $null)
      {
         Write-Host "Site Collection : " + $siteColl.Url
         foreach($subWeb in $siteColl.AllWebs)
          {
            if($subWeb -ne $null)
              {
                #Print each Subsite
                #Write-Host $subWeb.Url
                 Write-Host $subWeb
                 $subWeb.Dispose()
              }
           else
              {
                Echo $subWeb "does not exist"
              }
          }
            $siteColl.Dispose()
       }
     else
         {
            Echo $siteColl "does not exist"
         }
     }

}
 else
     {
       Echo $webApplicationURL "does not exist, check the WebApplication name"
     }
Remove-PsSnapin Microsoft.SharePoint.PowerShell
Echo Finish
});

Monday, 26 August 2013

Change the display name of a refiner in SharePoint 2013 Search Refinement Web Part.

By default, the name of the managed property will be used as a display name for the refiner. In many cases, the managed property name is hard to understand — for example, RefinableString00 or ColorOWSTEXT. You can fix this by changing the name of the refiner in a JavaScript file.
  1. On the page that contains the Refinement Web Part, on the Settings menu, click Site Settings.
  2. On the Site Settings page, in the Web Designer Galleries section, click Master pages and page layouts.
  3. On the Master Page Gallery page, click Display Templates.
  4. On the Display Templates page, click Language Files.
  5. On the Language Files page, click the folder for the relevant language.
  6. Open the CustomStrings.js file.


 Add one line for each managed property that you want to change the display name for. Use this syntax: 

"rf_RefinementTitle_ManagedPropertyName": "Sample Refinement Title for ManagedPropertyName"
For example, add this line to change the display name for the managed property RefinableInt00 to Price:

"rf_RefinementTitle_RefinableInt00": "Price".

  1. Save the file.

Next Post , I will discuss about, how to achieve this using power shell.

Friday, 2 August 2013

Sending SharePoint List Item Attachments in Email Programatically.


Hi All,

Got a new thing for me, to share with you all.
when you have to send email and attachment  with it then its easy task, when attachment file is lying on your file system.
But, when Attachment is as URL format, then its a little problem , as directly ULR formats are not supported.  In this case you have to go by Steam param method for Attachment.
We need to use WebClient  class for the same. with UseDefaultCredentials property set as true.
Otherwise you will get error 401 (Un-Authorized)

Below is code snippet.  you can direcly call this method. Good to go


     public static void SendMailWithAttachment(SPWeb web, string toAddress, string ccAddress, string subject, string body,SPListItem Itemattachment)
        {
            MailMessage objEmail = new MailMessage();
            MailAddress mailFrom = null;
            SmtpClient smtp = null;

            SPWebApplication webapp = web.Site.WebApplication;
            mailFrom = new MailAddress("Admin@abc.com", "Admin");
            objEmail.From = mailFrom;

// if toAddress Contains many email addresses seperated by ','
            if (toAddress.Contains(","))
            {
                string[] tos = toAddress.Split(',');
                for (int i = 0; i < tos.Length; i++)
                {
                    objEmail.To.Add(new MailAddress(tos[i]));
                }
            }
            else
            {
                objEmail.To.Add(new MailAddress(toAddress));
            }

// if ccAddress Contains many email addresses seperated by ','
            if (!string.IsNullOrEmpty(ccAddress))
            {
                if (ccAddress.Contains(","))
                {
                    string[] ccs = ccAddress.Split(',');
                    for (int i = 0; i < ccs.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(ccs[i]))
                            objEmail.CC.Add(new MailAddress(ccs[i]));
                    }
                }
                else
                {
                    objEmail.CC.Add(new MailAddress(ccAddress));
                }
            }

            objEmail.Body = body;
            objEmail.Subject = subject;
            objEmail.IsBodyHtml = true;


          //Adding Attachments to the Email, Itemattachment is SPListItem which contains Attachments

            foreach (string fileName in Itemattachment.Attachments)
            {
              string attachPath=  Itemattachment.Attachments.UrlPrefix + fileName;
              WebClient webAttach = new WebClient();
              webAttach.UseDefaultCredentials = true;
              var stream = webAttach.OpenRead(attachPath);
              Attachment attached = new Attachment(stream, fileName);
              objEmail.Attachments.Add(attached);

            }
            smtp = new SmtpClient();
            smtp.Host = webapp.OutboundMailServiceInstance.Server.Address;
            smtp.Send(objEmail);
        }