Tuesday 9 April 2013

2 More Code Snippets for SharePoint 2010


Hi all, here am about to discuss the usage of two APIs in Microsoft.SharePoint.dll (location: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI).
Using Microsoft.SharePoint.Administration;
Yesterday we had a discussion that our App should run on both SharePoint Foundation and SharePoint Server. For that thing, we need to detect the SharePoint products installed in a machine, as we are already aware of through Microsoft.SharePoint namespace we can manipulate the objects such as Sites, Webs, Lists, etc., Here we have to access the farm object, using Microsoft.SharePoint.Administration. Please, find this handy code, to do that thing.
Idea Behind:
For each of the SharePoint Product, MS had a GUID
// B2C0B444-3914-4ACB-A0B8-7CF50A8F7AA0 :SharePoint Server 2010 Standard Trial
// 3FDFBCC8-B3E4-4482-91FA-122C6432805C :SharePoint Server 2010 Standard
// 88BED06D-8C6B-4E62-AB01-546D6005FE97 :SharePoint Server 2010 Enterprise Trial
// D5595F62-449B-4061-B0B2-0CBAD410BB51 :SharePoint Server 2010 Enterprise
Hence, checking whether these guids are installed in our machine, will detect whether it’s a SharePoint Server machine.
Code:
Guid[] serverGuids = { new Guid(“B2C0B444-3914-4ACB-A0B8-7CF50A8F7AA0″),
newGuid(“3FDFBCC8-B3E4-4482-91FA-122C6432805C”),
newGuid(“88BED06D-8C6B-4E62-AB01-546D6005FE97″),
newGuid(“D5595F62-449B-4061-B0B2-0CBAD410BB51″) };
// If a Server ID cannot be detected we are running on Foundation.
boolisServer = SPFarm.Local.Products.Any(productGuid =>serverGuids.Contains(productGuid));
if (isServer)
{
Console.WriteLine(“You are running on SharePoint Server!!”);
}
else
{
Console.WriteLine(“You are running on SharePoint Foundation!!”);
}
Point to Remember:
The below will list out the SP products installed,
IEnumerable<Guid>mySP_Products = SPFarm.Local.Products;
Using Microsoft.SharePoint.WebPartPages;
For this, I need to discuss about a defect, which we faced at the last week, as we are showing the SharePoint Native Document Library, nothing but a page named AllItems.aspx [AllItems view] in an iframe of our App, as below

Wait, for the next set of items, even we passed IsDlg=0 for this page, next paging onwards, able to see, the ribbon, and Quick Launch too, as below

Solution:
The document container, which we are seeing is nothing but ListViewWebPart, as on editing the page, able to view, the properties for that web part as Asynchronous Update so on setting the value to true, able to get the page, without ribbon & quick launch. As we are having many doc libraries, we need the code, to do that,
C# Way:
SPSitesiteObj = new SPSite(your_siteurl);
SPWebwebObj = siteObj.OpenWeb(your_weburl,true);
SPLimitedWebPartManagerwebPartMgr = webObj.GetLimitedWebPartManager(“doc_lib_name/forms/AllItems.aspx”,WebParts.PersonalizationScope.Shared);
// get the XsltListViewWebPart…
// assumes the XsltListViewWebPart is the first and only one webpart present on that page
XsltListViewWebPartlvwp = (XsltListViewWebPart)webPartMgr.WebParts[0];
// set the webpart’sasync property.
lvwp.AsyncRefresh= true;
// save the changes
webPartMgr.SaveChanges(lvwp);
PowerShell Way:
functionenableAsync($siteUrl)
{
[Microsoft.SharePoint.SPSite]$siteObj = New-Object Microsoft.SharePoint.SPSite($siteUrl);
[Microsoft.SharePoint.SPWeb]$webObj = $siteObj.OpenWeb();
[string]$webPartUrl = -join($webObj.ParentWeb.Title,”/Forms/AllItems.aspx”);
Write-Host “WebPartUrl :: ” $webPartUrl;
[Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager]$webPartManager = $webObj.GetLimitedWebPartManager($webPartUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
[Microsoft.SharePoint.WebPartPages.XsltListViewWebPart]$lstViewWebPart = [Microsoft.SharePoint.WebPartPages.XsltListViewWebPart]$webPartManager.WebParts[0];
if($lstViewWebPart.ASyncRefresh -eq $false)
{
$lstViewWebPart.ASyncRefresh = $true;
$webPartManager.SaveChanges($lstViewWebPart);
Write-Host “Doc Lib titled — ” $lstViewWebPart.Title ” is async enabled now.”;
}else
{
Write-Host “Doc Lib titled — ” $lstViewWebPart.Title ” is already async enabled.”;
}
}
Happy Coding :)

No comments:

Post a Comment