Tuesday, February 21, 2012

Troubleshooting WCF and JSON serialization under SharePoint 2010 Context

Scenario:
To host a WCF service into SharePoint 2010 and surface its data using JSON serialization. After setting the service up and deploying it to SharePoint I run into a few issues before I could get any joy. This post will show you the problems I run across. 


NOTE: This post won't go into details about how to deploy a WCF service into SharePoint 2010. I'm planing to cover this in further detail in my next post.

Okay so, to start with here's the interface of the WCF method. The implementation of GetMessageJSON() is simply the almighty "Hello World!" hence why it's omitted.  

When I tried to browse the JSON method I got nothing but a blank HTML page. Fiddler helped me to discover there was exception there 400 Bad Request Exception.    


http://YourServer/_vti_bin/JqueryWCFRestSP/CustomerService.svc/GetJOSNMessage


However I could browse to the service WSDL with no problem and I could see its definition which appeared to be fine.


http://YourServer/_vti_bin/JqueryWCFRestSP/CustomerService.svc?wsdl


Strange, could get to the WSDL but can't get to the JSON method? Mmmmm, I knew there was nothing wrong with the WCF method (return "Hello World!" come on!) nor with its interface. So the actual problem was in the web.config this time as I was using basicHttpBinding which does not support JSON. Point taken, changed the binding  to webHttpBinding which does, redeployed the solution and problem gone. Next!


This time the WebRequest when through however I was getting the following response.


Later on I found out this can be due to the following reasons:
  • Using different contracts between client and sender.
  • Different binding between client and sender.
  • The message security settings are not consistent between client and sender.

As Brian McNamara correctly pointed out when web-hosted, the EndpointAddress of the service comes from the IIS base address (host headers).  In this case, it appears to be the "our-server" address.  By default, WCF ensures that the To of each Message matches the intended address.  If the service just has this one endpoint, a quick workaround would be to use 
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
which turns off the address filter.  Otherwise, ensure the IIS host headers contain the 'public facing base address' that you want clients to connect to, so that messages will be properly addressed to the service. 
Unfortunately, Brian's logical input only fixed part of the problem (Thanks Brian!) So what about the other part? Well, the credit this time goes to Kiyoshi Kusachi.He introduced a  "nested" multi behaviour approach which turned to be the key of success. After updating the web.config accordingly the AddressFilter mismatch at EndPointDispatcher vanished completely.  
Finally, I was able to serialize it successfully to JSON using the snippet (Console App).
WebClient proxy = new WebClient();

byte[] data = proxy.DownloadData("http://YourServer/_vti_bin/JqueryWCFRestSP/                                     CustomerService.svc/GetJOSNMessage");

Stream stream = new MemoryStream(data);DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));string result = obj.ReadObject(stream).ToString();
Console.WriteLine(result.ToString());
Console.ReadKey(true);

Time for lunch now. Happy WCFing!

Monday, February 20, 2012

SharePoint 2010 Error : Invalid assembly public key. (Exception from HRESULT: 0x8013141E)



I was banging my head against the wall while trying to figure out what on earth SharePoint was intending to tell me by "Invalid assembly public Key". 


Well, it turns out, I first created an isolated WCF service and just a few minutes later I decided to run it under the SharePoint Context (by deploying it to ISAPI mapped folder). The keyword attribute 'PublicKey' appeared to be the culprit as it doesn't get parsed correctly, using 'PublicKeyToken' instead will likely fix the problem.


Hope that'll save you some time.  

Friday, February 17, 2012

Migrating on-premise SQL Server Databases to SQL Azure

Migrate your existing on-premise databases to the cloud shouldn't give you many headaches *hopefully*. That's right, just follow the steps below to find out how easy it is.


1. Select the Database | Tasks | Generate Scripts...  



2. Provide the file name and path where the script is going to be saved to. Then, click on Advance.


3. Change the database engine type to script using SQL Azure Database 


Make sure you include the Database content too. By default is set to schema only.


And that's it really, the generated script is now ready to run on the SQL Azure! You can then import that script and run it to SQL Azure environment. In the next post I'll show the step by step how to achieve it. Easy, huh?


Thursday, October 06, 2011

Shrinking SharePoint_Config transactional log (when in emergency)

Some SharePoint critical errors thrown frequently and repeatedly might end up in an unexpected titanic growth of the SharePoint_Config transactional log. It can get seriously big, believe me... and the worst of all, it keeps growing and growing *fast*! 


This will lead us to an unstable working environment where nothing actually works as it should, slow, unresponsive and likely to be hit by collateral damage (i.e. Out of disk space just to name one). Well, my client fully suffer it the other day, the actual log hit the 1 TB mark! Woohoo!


So, no panic, before we get to diagnose and fix the cause of issue, let's quickly shrink the transactional log so we can get back to 'healthier' and quicker environment. 


Executing the following SQL script against the  SharePoint_Config database will compress the size of SharePoint_Config_log regardless of its size.

USE SharePoint_Config
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE SharePoint_Config
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (SharePoint_Config_log, 1);  -- here 2 is the file ID for trasaction log file,you can also mention the log file name (dbname_log)
GO
-- Reset the database recovery model.
ALTER DATABASE SharePoint_Config
SET RECOVERY FULL;
GO


From this point, the size of the log should be significally smaller. Hope you can can solve the issue/s now. Good luck!

Wednesday, October 05, 2011

Return only documents in search results

A few days ago my client came up with this new requirement where the SharePoint search should only display nothing but documents. Everything else will go ignored.

To achieve that I would recommend to create a Search Scope and then apply a set of rules that will basically prevent the unwanted items to show up.

The rules are:

*://*allitems.aspx*  Exclude
*://*webfldr.aspx*   Exclude
*://*mod-view.aspx*  Exclude
*://*my-sub.aspx*    Exclude
*://*/lists/*       Exclude
*://*lists*       Exclude

And here is how it should look once you entered them

    
For us, PowerShell entusiasts, here's how it looks  :)

write-host "Enter the Search Service Application Url where you need to apply the crawling rules:"
[string]$SearchServicerUrl = Read-Host #i.e. Enterprise Search Service Application

New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*webfldr.aspx*" -CrawlAsHttp 1 -Type ExclusionRule
New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*mod-view.aspx* " -CrawlAsHttp 1 -Type ExclusionRule
New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*my-sub.aspx*" -CrawlAsHttp 1 -Type ExclusionRule
New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*allitems.aspx*" -CrawlAsHttp 1 -Type ExclusionRule
New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*/lists/*" -CrawlAsHttp 1 -Type ExclusionRule
New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchServicerUrl -Path "*://*all forms.aspx*" -CrawlAsHttp 1 -Type ExclusionRule


Happy Powershelling!

Thursday, March 31, 2011

SharePoint 2010: Changing Regional Settings and Locale Settings in Powershell


I just had a client scenario where I have the SharePoint implementation has up to 40 different site collections to maintain. Well, I'm London based and so is my client... what about the default regional settings? Eng-US right? Is my client happy with it? Not really: en-GB, please!.
All right, first of all, let's change the Regional Settings at the level of the Web Application. That's the easy one, as it can be leveraged from SP "Central Admin" site. So simply follow the steps below:
  • Go to "Central Admin"
  • Click "Application management" from the quick launch.
  • Click "manage web applications" under "Web application" heading
  • Select web application on which you want to change the regional settings.
  • From the ribbon click "General setting" and select "General Settings" from the dropdown.
  • Setting modal dialog will appear, you can change the "default time zone" and other settings if you want to change, and click OK.
  • IISReset
And you are done! Wait! What about the locale settings? Aha, the mighty Powershell is happy [once more] to come to the rescue! The following script will do the dirty job for you. In a nutshell it iterates through each site collection within a given web application updating its locale settings and verifying right after if that has been changed successfully.


[Reflection.Assembly]::LoadWithPartialName("System.Globalization") | out-null
clear-host
$SiteCols = Get-SPWebApplication "<SiteCol URL>" | Get-SPSite -Limit ALL | Select URL
foreach ($siteCol in $SiteCols)
{
write-host "---- Site Collection: " + $siteCol.url + "----"

 

get-spweb -site $siteCol.url -limit all |% {
$_.Locale = [System.Globalization.CultureInfo]::GetCultureInfo("en-GB");
$_.Update()
}
#check if culture has been updated
get-spweb -site $siteCol.url -limit all |% {
$_.Locale
}
}


By the way, make sure you are running the script under a site collection administrator; otherwise you'll get a nasty Access Denied exception.


Get-SPWeb : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:4 char:12
+ get-spweb <<<< -site "<SiteCol URL>" -limit all |% {
+ CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], UnauthorizedAccessException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb


Make Powershell your buddy when it comes to pre-deploy | post-deploy tasks. It will save a lot time that's for granted, but also it will save you heaps of headaches when deploying SharePoint application between different environments.

Wednesday, February 02, 2011

Unable to add a lookup field using AddFieldAsXml –Solved!


I recently had a problem when trying to add a new lookup field programmatically. It turns out that Field ID was actually the culprit. You can't use a fixed field ID when using web.Fields.AddFieldAsXml(sColumnDefinitionXml) which is what the XML for a field is supposed to have when it is used as an element file in a feature, but not when you are adding the field using code.
Then:
Wrong XML definition for sColumnDefinitionXml


 

<Field Type=\"Lookup\" DisplayName=\"Chat Waiting Time\" Required=\"FALSE\" List=\"4c00bad4-e57d-417e-ad6e-d29fcad06c3f\" ShowField=\"AdvisorResultText\" Group=\"ChildLine Site Columns\" ID=\"{D4654C7B-7DDD-4c79-9CB1-7130B567C3BC}\" StaticName=\"ChatWaitingTimeLookUp\" Name=\"ChatWaitingTimeLookUp\" xmlns=\"http://schemas.microsoft.com/sharepoint/\" />"        

 

Right XML definition for sColumnDefinitionXml


 

<Field Type=\"Lookup\" DisplayName=\"Chat Waiting Time\" Required=\"FALSE\" List=\"4c00bad4-e57d-417e-ad6e-d29fcad06c3f\" ShowField=\"AdvisorResultText\" Group=\"ChildLine Site Columns\" StaticName=\"ChatWaitingTimeLookUp\" Name=\"ChatWaitingTimeLookUp\" xmlns=\"http://schemas.microsoft.com/sharepoint/\" />"        


Took it off and it worked like a charm. Now all lookup fields can be safely added by my feature receiver J.