April 9, 2012

Console application - accessing all webparts of a publishing page

Introduction:

Recently, we have been working on a sharepoint console app that finds invalid metadata terms assigned to web part properties. To get started we were trying to get all web parts on a publishing page.

Problem​:

There were several CQWP on page which were showing as error webparts when you get them using LimitedWebPartManager.

Solution:

Since CQWP and other publishing site web parts uses HttpContext.Current, we have to create a fake object in order to get it working properly. Here is the code that was included before GetLimitedWebPart call:


   1:  if (HttpContext.Current == null)
   2:  {
   3:  HttpRequest request = new HttpRequest("", web.Url, "");
   4:  HttpContext.Current = new HttpContext(request,
   5:  new HttpResponse(new StringWriter()));
   6:  HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
   7:  WindowsPrincipal wP = new WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent());
   8:  HttpContext.Current.User = Thread.CurrentPrincipal = wP;
   9:  }​
If you have any questions you can reach out our SharePoint Consulting team here.
Thanks​

April 6, 2012

Powershell: Get all web parts on a publishing page

There are lots of scripts available on web already for this. You must be wondering why am I posting this one?


When I tried various scripts on net to find web part on page, it gave me all content editor web parts on publishing page as error web part type.


After searching so much on internet I come through this link​ through which I was able to inspect correct type of web part. Here is the script:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")


$site = new-object Microsoft.SharePoint.SPSite "http://br41:19685/products/a-z-product-list"
$web = $site.OpenWeb()
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pWeb.PagesList
$file=$web.GetFile("Pages/sas-pm.aspx")
if($file.Exists)
{
$manager = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
$wps = $manager.webparts
$wps | select-object @{Expression={$pWeb.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”}, DisplayTitle, IsVisible, @{Expression={$_.RepresentedWebPartType.Name};Label=”Type”}
}
else
{
Write-Host "page not found"
}


Happy powershell scripting

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

April 2, 2012

How to force browser to clear cache for the Style sheet and Java Script

While googling on to the internet we may found so many solutions for forcing browser to clear the cache for the Stylesheet and javascript using code programatically.

But if we want to do it without any programming using IIS(Interner Information Server)?.

Follow given below steps to do this.

1.From the Start menu, click Administrative Tools, and then click Internet Information Services (IIS) Manager.

2.In the tree view on the left side, find your application.

3. Select the Output Caching menu item.




4. In the right column, click Add in the Action menu. You can add your output caching rule here.




5. In the File name extension field, for example, type .css, and then select User-mode caching and then click on Advanced button.



6. select the Query string variable(s) check box. And enter the appropriate variable(s) in the Query string variable(s) text box as *.



7. Follow this same procedure for javascript , add .js instead of .css in step 5.




Happy coding

Cheers!!!

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