Friday, February 27, 2015

Unable to Create a Search Managed Property

While i was trying to create a new Search Managed Property against a crawled property using either PowerShell or UI I am gettin an error "There was an internal problem connecting to or querying the database".

I spent enough time in figuring out the problem. At last I have identified that the Search Service Application doesn't have a valid Administrator who has access to SSA database.
Note: The mappings will be created in DB MSSManagedProperties table.

Administrator can be added by going to Central Administration --> Manage Service Applications --> Select Search Service Application --> Click on Administrators (Ribbon) --> Add an user with Full control (who has proper privileges to DB).

This process will provide the needful privileges to SSA to add the new search managed properties.

Why Office 365 APIs

What is Office 365 APIs:
Office 365 APIs are collections of tools that simplify building Office’s cloud services into your apps.

The Office 365 APIs are REST services that provide access office 365 services like mail, calendars, and contacts from Exchange Online; files and folders from SharePoint Online and OneDrive for Business; users and groups from Azure AD.

Why Office 365 APIs & Advantages of Office 365 APIs:
·    Extend Office everywhere.
·    General availability of new Office 365 APIs for mail, files, calendar and contacts.
·    Build on an Open Platform - Apps for mobile, web, and desktop.
·    Visibility for developers’ apps through the new Office 365 app launcher.
·    Office 365 and Azure Active Directory as hubs where apps store files and users build collaborative relationships with others.
·    Building the Office 365 platform on open standards such as HTML5, REST web services, and OAuth.
·    Microsoft is providing SDKs for common platforms to access o365 REST APIs includes mobile SDKs for native app development.
·    Microsoft is exposing O365 to apps by making call against REST APIs.
·    REST and OData are used to transfer data, JSON for data abstraction.
·    Authenticate users using single sign-on with Azure Active Directory (Azure AD) to access all office 365 services.
·    OAuth version 2.0 for authentication that means application does not have to handle or store user credentials and makes it possible to request tightly scoped permissions to user data.
·    Automatically determine URL of O365 services using discovery capability.
·    Office 365 App also supports multi tenants.
·    Support rich query filters.


Tuesday, January 6, 2015

How to Check Group Exists in SharePoint Programatically

I was trying to check if specific group exists in SharePoint web. I couldn't find any OTB API method to achieve this.
Hence I am using the below LINQ statement to check whether group exists in a web.
string groupName = "MyGroupName";
SPWeb web = SPContext.Current.Web;
 //Check if group exists in the web
 if (web.Groups.OfType<SPGroup>().Count(g=>g.Name.Equals(groupName, StringComparison.InvariantCultureIgnoreCase))>0)
 {
     //Get the SPGroup by group name if exists
     SPGroup group = web.Groups[groupName];
 }

Monday, December 1, 2014

Access Denied within SPSecurity.RunWithElevatedPrivileges

I never thought that SPSecurity.RunWithElevatedPrivileges gives an error "Access Denied". But yes, i see it in my below scenario.

I was doing an operation within an item event receiver which required higher level of access to update the web level information. Since this was to be executed irrespective of the access of the user logged in, i had the code to be executed within the SPSecurity.RunWithElevatedPrivileges. Unexpectedly the code was failing and throwing an error "Access Denied".

The below code executes well and fine for the users who has Full Control on a web but gives a "Access Denied' error for contributors or readers though the code is wrapped in SPSecurity.RunWithElevatedPrivileges.
public override void ItemCheckedIn(SPItemEventProperties properties)
{
            base.ItemCheckedIn(properties);
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                         SPWeb web = properties.Web;
                         var listItem = properties.ListItem;
                          if (!listItem.Title.Equals(web.Title))
                          {
                                    web.AllowUnsafeUpdates = true;
                                    web.Title = listItem.Title;
                                    web.Update();
                                    web.AllowUnsafeUpdates = false;
                           }
             }
}
In the above code web.Update() throws an exception "Access Denied" Though the code is executing under RunWithElevatedPrivileges. Because the code doesn't really execute under elevated privileges as the respective object SPWeb was not created within the SPSecurity.RunWithElevatedPrivileges but it is retrieved through the receiver context properties.Web, which is created before elevated privileges. Hence the elevated privileges cannot be applied to that object.

The solution for this problem is very simple. Create the objects which are being used to perform some operations with in the SPSecurity.RunWithElevatedPrivileges.
public override void ItemCheckedIn(SPItemEventProperties properties)
{
          base.ItemCheckedIn(properties);
          SPSecurity.RunWithElevatedPrivileges(delegate()
          {                 
                 var listItem = properties.ListItem;
                 using (SPSite site = new SPSite(properties.Site.ID))
                 {
                        using (SPWeb web = site.OpenWeb(properties.Web.ID))
                        {     
                               if (!listItem.Title.Equals(web.Title))
                              {
                                    web.AllowUnsafeUpdates = true;
                                    web.Title = listItem.Title;
                                    web.Update();
                                    web.AllowUnsafeUpdates = false;
                              }
                        }
                 }
           }
}
In this case created a SPWeb object with in elevated previliges though it is available as properties.Web.

Wednesday, November 19, 2014

PowerShell Updating the Page Layout of a Publishing Page

Here is the simple and efficient PS script to update the page layout of a page, if you have both page and layout urls.

While changing the page layout of a publishing page in SharePoint, make sure that the page is of the same content type that is associated with the page layout.
#Add sharepoint snapin
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

#Get publishing web, site and page
$spWeb = Get-SPWeb("http://sampleweb.com")
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
$pSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($spWeb.Site);
$pubPage = $pWeb.GetPublishingPage("http://sampleweb.com/Pages/testpage.aspx") 

#Get page layout
$siteLayouts = $pSite.GetPageLayouts($false)
$myLayout = $siteLayouts["/_catalogs/masterpage/mylayout.aspx"] 

#Update page layout of a page
$pubPage.CheckOut() 
$pubPage.Layout = $myLayout 
$pubPage.ListItem.Update() 
$pubPage.CheckIn("Updated page layout via PowerShell")

$spWeb.Dispose()