Wednesday, March 20, 2013

SharePoint 2013 configure People Search using PowerShell Scripts

Hi guys in this post I'm going to show how can we configure people search using PowerShell. There are some prerequisites for people search.

People search prerequisites:
  • Search Service Application should be running in the SharePoint farm. 
  • SharePoint farm also should have a Search Center. (Enterprise Search Center)
  • Managed Metadata Application should be running in the SharePoint farm. 
  • User Profile Service should be configured and running in the farm.
  • MySite Should be configured.
So check the above given prerequisites are working in your SharePoint farm.

These are the steps to configure people search:

  • Check whether your user account has administrator privileges on both Search Service Application and User Profile Service Application.
  • Create a new crawl rule 
          #Creating a crawl rule for MySite Web Application
          $Path = "sps3://<host name>"
          $UserName = Read-Host "Enter Username"
          $Password = Read-Host "Enter Password" -AsSecureString
          $SearchApp = Get-SPEnterpriseSearchServiceApplication
          $Rule = New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchApp -Path $Path -Type    InclusionRule -AuthenticationType BasicAccountRuleAccess -AccountName $UserName -AccountPassword $Password 
          $Rule.Update()
          Write-Host -f Green "New Rule created"

Before you run this script specify the Url of the Web Application where MySite is deployed (Url format is: sps3://<host name>) for $Path variable. It'll prompt for user authentication give the correct user credentials which has the administrator privileges.

  • Remove the Url of the Web Application where MySite is deployed from the default Content Source (Local SharePoint Sites)
          #Removing the MySite Url from the default Content Source
          $search = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
          $contentSource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $search -Identity "Local SharePoint Sites"
          $addresses = $contentSource.StartAddresses
          $addresses.Remove($Path)
          $contentSource.Update()
          Write-Host -f Green "Web Application Url of MySite successfully removed from default content source"

Before you run this script specify the name of your Search Service Application for $search variable.

  • Now create a new Content Source with Url that you removed from the default Content Source.
          #Creating a new content source for MySite
          New-SPEnterpriseSearchCrawlContentSource -SearchApplication $search -Type SharePoint -name   "MySite" -StartAddresses $Path 
          Write-Host -f Green "New content source is successfully created for MySite"

After you run this script new Content Source will be created with the name "MySite". Go to manage content sources in Search Service Application, click on the newly created Content Source and click "Start Full Crawl".  

Now good to go people search is successfully configured.

Feel free to give your feed backs.
Cheers.

Luckshan Roy.

Friday, March 8, 2013

Sharepoint 2013 creating a new discussion on discussions list

Hi guys have you all ever tried creating a new discussion on discussions list programmatically?? I tried recently and got an issue. Like we all normally create and add an item  to a list I tried adding a discussion item on discussions list. Well it got created as the way I want but when I tried to reply to that discussion a message box popped and gave this following error message.

"Value does not fall within the expected range"

This is the C# code I used to create and add a new disussion on discussions list.


using (SPSite site = new SPSite("http://site-url"))
{
          using (SPWeb web = site.OpenWeb())
          {
                    SPList list = web.Lists["Discussions List"];
                    SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);

                    SPListItem item = list.Items.Add();
                    item["Title"] = "New Post";
                    item["Body"] = "Body of the post";
                    item.Update();
          }
}

This is the disussion got created



This is the error message box popped out when I tried to reply that discussion


So when I checked the log file it gave me this error message.

Failed to cache field with id "{1805e563-22cf-44ed-96f5-58ebb8a6cb80}", overwrite=0

SocialRESTExceptionProcessingHandler.DoServerExceptionProcessing - SharePoint Server Exception [System.ArgumentException: Value does not fall within the expected range. at Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussionReply(SPListItem parent) at Microsoft.SharePoint.ServerStub.Utilities.SPUtilityServerStub.InvokeStaticMethod??(String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid) at Microsoft.SharePoint.Client.ServerStub.InvokeStaticMethodWithMonitoredScope(Stri??ng methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)] 

Answer

I did some research and got some online help from others and found the solution. Discussions list is a type of list but we can't add items to it like we add on other lists. So there is a different way of adding the discussions on discussions list. This is working code:

using (SPSite site = new SPSite("http://site-url"))
{
          using (SPWeb web = site.OpenWeb())
          {
                    SPList list = web.Lists["Discussions List"];
                    SPListItem listItem = SPUtility.CreateNewDiscussion(list, "New Post");
                    listItem["Body"] = "Body of the post";
                    listItem.Update();   
          }





Cheers :D
Luckshan Roy