tag:blogger.com,1999:blog-63632884003682370152024-03-13T06:32:32.462-04:00SharePoint Learning CurveA journey of learning SharePoint 2013, from the viewpoint of a SharePoint 2010 professional, previously just a SharePoint 2007 professional.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.comBlogger46125tag:blogger.com,1999:blog-6363288400368237015.post-4210694113284014562013-08-13T13:54:00.000-04:002013-08-13T16:32:12.548-04:00Creating a custom WCF REST service for SharePoint 2013 without a Visual Studio template<h2>
Overview</h2>
Recently a colleague asked me how I built REST services for SharePoint 2013, since there is no template for this in Visual Studio 2012. While I have worked with some of the community built templates in the past, they generally require installing additional things on your development server, which really isn't necessary. You can actually stand up a REST service in SharePoint 2013 rather quickly, with just Visual Studio 2012.<br />
<br />
Let's start with a video walking you through the process. The video follows the process outlined further down the post. If you want to view the full resolution video, you can download it from <a href="https://www.dropbox.com/s/l013sj4yteq1l1r/SharePoint%20WCF%20REST%20Service.mp4">Dropbox</a>. You can also download the code built in the video from <a href="https://www.dropbox.com/s/d50ak40zk1zo5od/MyRestService.zip">Dropbox</a>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="270" mozallowfullscreen="" src="http://player.vimeo.com/video/72284775" webkitallowfullscreen="" width="500"></iframe> <br />
<a href="http://vimeo.com/72284775">Creating a WCF REST service for SharePoint 2013</a> from <a href="http://vimeo.com/user19519075">Mike Winburn</a> on <a href="https://vimeo.com/">Vimeo</a>.
</div>
<br />
<h2>
Creating the REST service</h2>
The basic process for creating your REST service are the following steps, which were demonstrate in the video.<br />
<br />
<ol>
<li>Create a new SharePoint 2013 Empty Project as a Farm Solution</li>
<li>Add a WCF Service Application project to your solution</li>
<li>Add the ISAPI mapped folder to your SharePoint project</li>
<li>Drag the Service1.svc and Service1.svc.cs files from the WCF project to the SharePoint project under the ISAPI mapped folder</li>
<li>Drag the IService1.cs file from the WCF project to the SharePoint project</li>
<li>Add the WCF references to your SharePoint project</li>
<ul>
<li style="margin-left: 10px;">System.Runtime.Serialization</li>
<li style="margin-left: 10px;">System.ServiceModel</li>
<li style="margin-left: 10px;">System.ServiceModel.Web</li>
</ul>
<li>Change the namespace from WcfService1 to your assembly's namespace (e.g., MyRestService) in Service1.svc.cs and IService1.cs</li>
<li>Build your project</li>
<li>Using the Visual Studio Command Prompt, go to the project output folder of the SharePoint project</li>
<li>Get the PublicKeyToken for your assembly using the strong name utility</li>
<ul>
<li style="margin-left: 10px;">sn -T MyRestService.dll</li>
</ul>
<li>In Service1.svc, change the Service attribute on the ServiceHost directive to be in the following format:</li>
<pre class="brush:csharp"><%@ ServiceHost Language="C#" Debug="true" Service="{NameSpace}.{ServiceName}, {AssemblyName}, Version=1.0.0.0, Culture=Neutral, PublicKeyToken={PublicKeyToken}" CodeBehind="Service1.svc.cs" %>
</pre>
<ul>
<li>Replace the tokens here, represented by {Token} with the appropriate values for your project. e.g., </li>
</ul>
<pre class="brush:csharp"><%@ ServiceHost Language="C#" Debug="true" Service="MyRestService.Service1, MyRestService, Version=1.0.0.0, Culture=Neutral, PublicKeyToken={PublicKeyToken}" CodeBehind="Service1.svc.cs" %></pre>
<li>Deploy your solution</li>
<li>Verify the service loads</li>
<ul>
<li><span style="color: red;">Note: see below for troubleshooting a common error.</span></li>
</ul>
<li>Add a HelloWorld operation to your contract, IService1.cs that is set up for REST.</li>
<pre class="brush:csharp">[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "HelloWorld")
string HelloWorld();
</pre>
<li>Implement the operation in Service1.svc.cs</li>
<pre class="brush:csharp">public string HelloWorld()
{
return "Hello World";
}
</pre>
<li>Update web.config with the appropriate info for your service</li>
</ol>
<pre class="brush:csharp"><behaviors>
<serviceBehaviors>
<behavior name="Service1ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyRestService.Service1" behaviorConfiguration="Service1ServiceBehavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="MyRestService.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</pre>
<ol>
<li>Test your HelloWorld operation and you should see the following:</li>
</ol>
<div class="separator" style="border: solid 1px black; clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFP_n0b_-M_Pham4bMQ7ZuHAu_pjvR9NeAj_i20D4uRbsfJqc6PZK_dYnQS2tCVtHaP36pwRGePAwPh4Zhxgn_4OFxHs9Qv290HvMMpKMh8XwU8pat6QaRbRQp7iuJtEC23xT7MFkZdP0/s1600/helloworld.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="146" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFP_n0b_-M_Pham4bMQ7ZuHAu_pjvR9NeAj_i20D4uRbsfJqc6PZK_dYnQS2tCVtHaP36pwRGePAwPh4Zhxgn_4OFxHs9Qv290HvMMpKMh8XwU8pat6QaRbRQp7iuJtEC23xT7MFkZdP0/s320/helloworld.png" width="320" /></a></div>
<br />
<br />
<h2>
Troubleshooting</h2>
<ul>
<li>If you get an error that says "<span style="color: red;">This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.</span>" this is due to having multiple bindings in IIS, which is common in SharePoint when you have multiple Alternate Access Mappings.</li>
</ul>
<div class="separator" style="border: solid 1px black; clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhH0TNPuyYsN-lEwCZZpgsu0Rwpc04-V3HmyzIMyV7gmw71zK-CMaADBimVXmzd_DDTIwmQzcWd2ttRnO1p2R0d7w8VjAKTVow5RJHnkTOflPGBRK8_2H31KQ2uQsxSd7nGWaXXZZib7-U/s1600/WcfError.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="This collection already contains an address with scheme http. There can be at most one address per scheme in this collection." border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhH0TNPuyYsN-lEwCZZpgsu0Rwpc04-V3HmyzIMyV7gmw71zK-CMaADBimVXmzd_DDTIwmQzcWd2ttRnO1p2R0d7w8VjAKTVow5RJHnkTOflPGBRK8_2H31KQ2uQsxSd7nGWaXXZZib7-U/s1600/WcfError.png" title="" /></a></div>
<ol>
<ul>
<li style="margin-left: 10px;">In web.config, add the <span style="color: red;">multiplesitebindingsenabled </span>attribute to the servicehostingenvironment element.</li>
<pre class="brush:csharp"><servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true"%></servicehostingenvironment%>
</pre>
</ul>
</ol>
Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com2tag:blogger.com,1999:blog-6363288400368237015.post-41541834375742075922013-05-13T14:32:00.000-04:002013-08-13T14:32:26.878-04:00SharePoint 2013 Searches return no results even though content sources were crawled successfullyRecently, a client of mine using SharePoint 2013 changed the URL of their site. A couple of weeks later, they noticed that search was not returning any results. I checked in ULS and noticed some errors indicating that the Content Source had not been updated with the correct URL. I made the change in search administration and did a recrawl. The crawl found several thousand items and had no errors.<div>
<br /></div>
<div>
Unfortunately, we still weren't getting any search results. The problem didn't appear to be with the index, but I cleared the index and rebuilt it just to be sure. Still no results. I also noticed that there was nothing useful in ULS to help me track down the problem.</div>
<div>
<br /></div>
<div>
On a suggestion from a colleague, I created a new web application and associated it with the same search service application. I added some dummy content to that new web application and indexed it. When I ran a search from within that search application, I was getting results. Progress - now I knew that my search service application appeared healthy.</div>
<div>
<br /></div>
<div>
We decided to see if the problem was the content or the site itself. We took a backup of the content from the production web application and restored it to the new web application we had just created. We then rebuilt the index and ran a test. Search worked perfectly. So our problem wasn't the content.</div>
<div>
<br /></div>
<div>
Now that we had narrowed down the problem to our web application, we had a pretty easy fix. We waited for a maintenance window and then removed the web application. We then provisioned a new web application with the correct settings for our production environment and attached the content database from the original. After another rebuild of our index, we had working search results.</div>
Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-23400317100819523532012-11-16T09:35:00.000-05:002012-11-16T09:35:10.961-05:00Difficulties with SP2013I'm a little late to the party this time, but I've finally carved out some time to play with SharePoint 2013. I've noticed two things so far:<br />
<ol>
<li>SharePoint 2013 chews CPU a lot more than SharePoint 2010 did.</li>
<li>The install has been flaky for me, perhaps due to point 1.</li>
</ol>
I've had several failed installs so far and they present weird errors when they do. The first failure I had wouldn't let me create managed accounts. I did a reinstall and failure number two wouldn't provision site collections correctly .<br />
<br />
The site I created using the wizard resulted in this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVKB6bMkA9mxZ6hhgPiHaaFxEsjw9SBX1o2NfsjSYt3FpoTMu5KIuI4WM0ozDdnupMl2DDRAqC0-6085cU9ZZXDGHQSlOsmn5UtstaLck5379J6USVVQwBUn0Zi_5Z_-B5_rjvQzqMQrc/s1600/25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVKB6bMkA9mxZ6hhgPiHaaFxEsjw9SBX1o2NfsjSYt3FpoTMu5KIuI4WM0ozDdnupMl2DDRAqC0-6085cU9ZZXDGHQSlOsmn5UtstaLck5379J6USVVQwBUn0Zi_5Z_-B5_rjvQzqMQrc/s400/25.png" width="400" /></a></div>
<br />
I created a new web application and then got this during the site collection creation:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8TUpP_b3OlUILdsXVikUdI0J5Sc-QECLoZK80nA2RV1jbanFUWXP6677l5APoaGJFQ8pnz7Vn2ep9e4Kc3cRl4cAmp3441Ix6xukBLL0n6098dUPkpTdt9x9x69yufKMnT9XrEduyVKo/s1600/26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8TUpP_b3OlUILdsXVikUdI0J5Sc-QECLoZK80nA2RV1jbanFUWXP6677l5APoaGJFQ8pnz7Vn2ep9e4Kc3cRl4cAmp3441Ix6xukBLL0n6098dUPkpTdt9x9x69yufKMnT9XrEduyVKo/s400/26.png" width="400" /></a></div>
<br />
That Correlation ID led to this error in the logs:<br />
<br />
<blockquote class="tr_bq">
A large block of literal text was sent to sql (length 93794). This can result in blocking in sql and excessive memory use on the front end.</blockquote>
<br />
I did three things that seem to have addressed the issue:<br />
<ol>
<li>Uninstalled SharePoint and deleted the 14- and 15-hives.</li>
<li>Upped the RAM on my dev box from 4Gig to 8Gig.</li>
<li>Reinstalled SharePoint</li>
</ol>
Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-46007062650696498372010-11-29T18:08:00.000-05:002010-11-29T18:08:49.578-05:00SharePoint 2010 - Replace missing Publishing Site Columns and Content TypesWhile working on a new content type that inherited from Article Page, I somehow lost a lot of Site Columns. When I tried to view the Article Page content type, it had no columns listed!<br />
<br />
This ended up being a simple fix. Run the following command to reinstall the Site Columns and Content Types that are used by Publishing:<br />
<br />
stsadm -o activatefeature -name PublishingResources -url http://MySharePointSite -force<br />
<br />
Once that operation completes, the missing columns should now be back. Don't forget to fix the feature that caused the problem!Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-19174328908893216822010-11-24T10:32:00.000-05:002010-11-24T10:32:23.805-05:00SharePoint 2010 - Content Deployment Woes Part 2: Custom FeaturesIf you are reading this post, please read through my <a href="http://sharepointlearningcurve.blogspot.com/2010/11/sharepoint-2010-content-deployment-woes.html">previous post</a> on this topic to get some background on tricks to solving the "Could not find Feature" issue with built-in SharePoint features.<br />
<br />
If you are still reading, then you have probably built and deployed custom features to your farm, such as a web part. You are then running into problems during Content Deployment jobs between farms that have different SharePoint versions on them, such as deploying from Enterprise to Standard.<br />
<br />
The solution that worked for the built-in features was appropriate because the features that were causing the problems didn't need to be installed on your destination farm. But with custom features, that is not the case. You built them precisely because you need to be able to run them on the destination farm.<br />
<br />
I encountered this same problem and it took me a little while to figure out why it was unhappy. Here are the steps I'd suggest you follow:<br />
<br />
<ul><li>Check the 14-hive on the destination Web Front End to verify your feature is there.</li>
<li>Verify that your feature is installed in the destination site. For instance, check the Site Features page for features scoped to the Site level</li>
<li>Try enabling the feature and making sure it doesn't thrown any errors</li>
</ul><br />
Maybe you've done all those things and the feature looks good, but it's still not working? At this point, you should be confused. I know I was. I decided to check the settings on my package in Visual Studio. The Deployment Server Type was set to WebFrontEnd, which was correct. I then checked the 14-hive on the destination App Server, which is where Content Deployment jobs are run. No feature folder there.<br />
<br />
Now I know what you're thinking - isn't that what we want? Solutions that are targeted as WebFrontEnd are marked that way for a reason - they don't need to be on the App Server because it's not serving up those web parts. Well, when it comes to Content Deployment, apparently they <b>DO</b> need to be there. I assume this is because the Content Deployment job is going through Central Administration, which is living on your App Server.<br />
<br />
The solution I came up with was to enable the Microsoft SharePoint Foundation Web Application service on my App Server, effectively making it a Web Front End as well. This ensures that when I add solutions to my farm that the features get deployed to my App Server as well, effectively squashing the Content Deployment job errors I was receiving.<br />
<br />
My App Server is already segmented off from the Web Front Ends by a firewall to keep it from being accessible from the internet. However, to ensure that my App Server is never used as a Web Front End, I am also making sure it is never listed in the load balancer and I am blocking traffic to port 80 on that box. This means there should be no real impact to the machine and my Content Deployment jobs can now run successfully.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-45263533874798242392010-11-24T10:19:00.000-05:002010-11-24T10:19:34.818-05:00SharePoint 2010 - Content Deployment WoesLately I've been working a lot with Publishing sites, which means I've been using Content Deployment jobs to move content between my farms. Unfortunately, I've learned the hard way that this part of SharePoint is rather particular about farm setups.<br />
<br />
I have an Enterprise development farm that I use for consulting work. I built some pages on it and then wanted to deploy them to my customer's test farm, which runs a Standard license. This broke on me every single time. Specifically, I kept getting errors that features didn't exist on my destination farm.<br />
<br />
One message I got was "Could not find feature IPFSSiteFeatures". Here are the screenshots from my deployment report: <br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiac3ZwsmLKBrVxQp4no7eB3cWzvuaDE1rKJw9GrtH0eYzhqHWZ9mN8Tvo5azimXbJizuGqazSyk4gXWph1Y1D0CGA46qpCD1fDytGqdNTd9pQLDXshe-G9aR22t1ulthnD23C4kfQKhbk/s1600/4.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiac3ZwsmLKBrVxQp4no7eB3cWzvuaDE1rKJw9GrtH0eYzhqHWZ9mN8Tvo5azimXbJizuGqazSyk4gXWph1Y1D0CGA46qpCD1fDytGqdNTd9pQLDXshe-G9aR22t1ulthnD23C4kfQKhbk/s1600/4.png" /></a></div><br />
<br />
<div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbUSpf5leZG1gjkRfVpjMwsv9HRmMUAKodiJ2O2m_QVbA0DbgGVKqffLhgIqPOyVmA3bk-abph5sEynpiaObVIDseWRnSbML0DjN-M3X8szDfu5SETeO7vGivg-64kkDzs9Uq8oZ0yFeo/s1600/3.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="32" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbUSpf5leZG1gjkRfVpjMwsv9HRmMUAKodiJ2O2m_QVbA0DbgGVKqffLhgIqPOyVmA3bk-abph5sEynpiaObVIDseWRnSbML0DjN-M3X8szDfu5SETeO7vGivg-64kkDzs9Uq8oZ0yFeo/s640/3.png" width="640" /></a></div><br />
<br />
<br />
<br />
These are all the features that existed on my Enterprise development farm but not on my Standard test farm:<br />
<ul><li>IPFSSiteFeatures</li>
<li>WACustomReports</li>
<li>PPSWorkspaceCtype</li>
<li>PPSMonDatasourceCtype</li>
<li>PPSWebParts</li>
<li>PPSSiteCollectionMaster</li>
</ul>To get my content deployment working again, I enabled them temporarily on the destination farm, performed the deployment, and then disabled them again. You might also be able to disable/uninstall those features on your Enterprise farm before doing the content deployment job and achieve success, but I didn't test that option. <b> </b><br />
<b>Note</b>: Make sure you disable these features on your destination farm after deployment, or you may be in violation of your SharePoint license.<br />
<br />
If you made the mistake of installing Excel Services and PerformancePoint Services on your Enterprise environment, your problems are more difficult. If you run <b>Get-SPFeature</b>, you will see more items in the list that will cause your content deployment to fail, such as BizAppsListTemplates.<br />
<br />
You might be tempted to disable them on your source farm. Unfortunately, when I tried, I learned you cannot disable them with PowerShell because they are still in use. However, those features aren't activated on any of my sites. Because of my failure to disable them on the source farm, I did not test enabling them on the destination farm because I wasn't sure I'd be able to get rid of them and I didn't want to have to rebuild the entire farm again and lose my content.<br />
<br />
I believe these features might be activated inside the service application itself, but I have not found a way to confirm this theory. I tried deleting the Service Application, but the features still couldn't be removed. If you figure this out, let me know.<br />
<h4>Summary</h4>In short, here are the things to keep in mind if you want to do a Content Deployment job between two farms:<ul><li>Source and Destination farms should be on the same version of SharePoint</li>
<li>You must deploy solutions that exist on your source farm to your destination farm prior to running the job.</li>
<li>Features that are enabled on your source farm will automatically get enabled on your destination farm, but if you encounter an error with a feature, verify that the feature is installed on the destination</li>
</ul>There are some additional quirks if you receive the "Could not find feature X" for custom features that you have developed. I'll cover those in a followup post. <br />
<ul></ul>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-51726245990922519362010-10-06T11:42:00.000-04:002010-10-06T11:42:58.115-04:00SharePoint 2010 - Dynamic JavaScript problems with Inline EditingSharePoint 2010 allows inline editing of your HTML on Publishing pages, which is a nice boost to productivity. However, there are some quirks with this feature that can really trip you up if you aren't ready for them.<br />
<br />
Here's a scenario I ran into this week. I have a simple Publishing Page with a Content Editor Web Part on it. Inside this web part, there is an HTML element that I target with some jQuery to display tooltips. The JavaScript here is really quite simple and just dynamically adds a relative positioned div to the page above the target element.<br />
<br />
The code for this is tested and works fine outside of SharePoint. But when you edit a page, you'll find an interesting side-effect. Specially, when editing the page, the JavaScript that adds the dynamic elements to the DOM still runs. When the page is saved, even if you didn't even edit that particular Content Editor Web Part, the updated DOM elements get saved - including the tooltip changes!<br />
<br />
I have tested this with a lot of scenarios, but it's pretty consistent across any SharePoint components that provide inline editing.<br />
<br />
So, how do you allow JavaScript to dynamically update content on publishing pages? This is really going to depend on the nature of your content. Here are a few different scenarios I've employed in my site:<br />
<ul><li>Extract the dynamic content and make it a web part.<br />
<br/>This works well if your content doesn't change that often and has few properties. A great example here would be having a web part that uses swfobject.js to load a flash file rather than placing the script call inside a Content Editor</li>
<li>Skip the JavaScript calls that update your page when in edit or design mode.<br />
<br/><br />
This works really well for scenarios like my tooltip. You can do this either in JavaScript or C#, depending on how your page works.<br />
</li>
</ul>Here is an example of checking your page mode with JavaScript, using jQuery:<br />
<pre class="brush:csharp">if ($('#MSOSPWebPartManager_DisplayModeName').val() == 'Design' || $('#MSOSPWebPartManager_DisplayModeName').val() == 'Edit') {
alert('Don't do anything');
} else {
alert('Safe to run JavaScript');
}
</pre>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-57813787010669581082010-07-31T14:04:00.000-04:002010-07-31T14:04:20.828-04:00SP2010: Unable to publish workbook to Excel ServicesWhile trying to publish a workbook to SharePoint 2010 to use with Excel Services, I ran into some problems. Specifically, it appeared that Excel wasn't able to access my Document library. <br />
<br />
To publish my workbook, I followed instructions you might find anywhere on the web, using the Office 2010 Backstage to save to SharePoint. However, none of my libraries showed up by default. Not a problem! I can just type the URL into the address bar, right? Apparently Excel says that it "can't open this location using this program".<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrTbYBlM3Yv1e4Yhznl1e3ec2P0yEE3v7MK2euAfCXx5JM5WNJMtONqhC7D7eHg1udZYz1OhQ72TZ8P2Ln1VWOM-JYMOHSX4nggxzdhWo7s_xkJg-HBPFvHjQXZqmGxeFOJGfemBT-QZg/s1600/es_5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrTbYBlM3Yv1e4Yhznl1e3ec2P0yEE3v7MK2euAfCXx5JM5WNJMtONqhC7D7eHg1udZYz1OhQ72TZ8P2Ln1VWOM-JYMOHSX4nggxzdhWo7s_xkJg-HBPFvHjQXZqmGxeFOJGfemBT-QZg/s320/es_5.png" width="320" /></a></div><br />
Generally problems like this are permissions related. Well, I was logged into the machine as my farm administrator, who has full rights to the Documents library. I was also able to click on files from SharePoint and have them open Excel automatically. <br />
<br />
I decided to take a new approach. I opened the demo Excel workbook that came with the Business Intelligence Center template. I then made a small update and saved it back to SharePoint. Now when I opened Backstage to save to SharePoint, the Documents library showed up in the Recent Locations section.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglE2BHZlB1afAg6MlZd9iDo4h0zJhv4n6GtqWd-YAO3y57OfmeBBtOwD98FoBTcuYUotR4uHsF5xDABbqMGv44i-4n_Cxza1-RqY7PHJlL7PV-DVnjy6Am4otemAxHMaIaoazagfUO9lU/s1600/es_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="273" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglE2BHZlB1afAg6MlZd9iDo4h0zJhv4n6GtqWd-YAO3y57OfmeBBtOwD98FoBTcuYUotR4uHsF5xDABbqMGv44i-4n_Cxza1-RqY7PHJlL7PV-DVnjy6Am4otemAxHMaIaoazagfUO9lU/s400/es_2.png" width="400" /></a></div><br />
I was hoping by doing this, I could trick Excel into finding the location. So I then tried to save my new workbook by just clicking on that recent location. However, Excel was still not able to do it, and told me it was unable to open my site:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnO5Z34Nde1J2QHkzdfhzTaWtBqJxtB8yNjs2YDCmROS9GO6TQjOh9yk7KeUtN2yH6mfKllAPqDztiV6IizwMU1UyqzciYpePy1dIjyJu_EAasTS9OtqH-wPMx6L_E4OL6SkO8gojhnzQ/s1600/es_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnO5Z34Nde1J2QHkzdfhzTaWtBqJxtB8yNjs2YDCmROS9GO6TQjOh9yk7KeUtN2yH6mfKllAPqDztiV6IizwMU1UyqzciYpePy1dIjyJu_EAasTS9OtqH-wPMx6L_E4OL6SkO8gojhnzQ/s320/es_3.png" /></a></div><br />
After looking around on the web, I found a <a href="http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010general/thread/e717c93c-73b3-4aa6-81e7-6d3d95ae2a03/">forum post</a> that seemed promising, as I'm using a Windows Server 2008 R2 machine. I opened Server Manager and installed the Desktop Experience, which required me to install the Ink and Handwriting Services as a prerequisite. I was then prompted to reboot upon completion. After rebooting, I attempted to save my workbook to SharePoint again and it worked.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com2tag:blogger.com,1999:blog-6363288400368237015.post-34526693805628010862010-07-31T13:26:00.002-04:002010-08-02T12:22:47.906-04:00SP2010: Excel Services Error - Unable to Process the RequestI've been playing a lot with Excel Services for the last week and while it is nice, it is also temperamental. Most of this can be chalked up to inexperience on my part as I discover the closest to least privileges you can get for a SharePoint 2010 Excel Services Service Application, in light of the ever popular bug, "<a href="http://blogs.msdn.com/b/jjameson/archive/2010/05/04/the-workbook-cannot-be-opened-error-with-sharepoint-server-2010-and-tfs-2010.aspx">The workbook cannot be opened</a>". However, I'm also convinced it's a little more particular than the previous version.<br />
<br />
By default when I installed Excel Services using a PowerShell script, it had an entry for a Trusted File Location at "http://". After playing with the Business Intelligence Center template for a bit, I created my own workbook that had a PivotTable and a PivotChart that talked to my SSAS installation. Naturally I decided to update my Trusted File Location settings. So I deleted the default entry and created a more specific one that pointed directly to the Documents library that came with the template. Then I checked on the Excel example that came with the template and found that Excel Services was no longer working because it was "Unable to process the request".<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_QlHKLrP1CBL-gJrJQSMFIYx-SyBMM9nIJj5CK4yyV1GWPST0Q1Dzu8HOHouZEif8A_HPyNH2jPenmXaGYzir_nU9hwXdsSZscDQVaWbSedzG3oN8F0IhIklc0fgEmVhXtkAISmKSPM/s1600/es_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="285" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio_QlHKLrP1CBL-gJrJQSMFIYx-SyBMM9nIJj5CK4yyV1GWPST0Q1Dzu8HOHouZEif8A_HPyNH2jPenmXaGYzir_nU9hwXdsSZscDQVaWbSedzG3oN8F0IhIklc0fgEmVhXtkAISmKSPM/s400/es_1.png" width="400" /></a></div><br />
Event Viewer showed nonstop critical errors and ULS had some gems in there pinning the blame on the Secure Store Service: "Request for security token failed with exception". I tried refreshing the key figuring that my WFE and App server were out of sync, but that didn't fix the problem. Lacking a better idea, I did an IIS reset on the WFE - and it started working again.<br />
<br />
I'm not sure why it was unhappy, but at least it was only a 10 min troubleshooting span and a simple fix!Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-84955551302107693962010-07-26T11:48:00.000-04:002010-07-26T11:48:00.296-04:00SharePoint 2010 - Error Removing Managed AccountRecently, while trying to experiment with Excel Services in SharePoint 2010, I decided to remove the service and do a reinstall with a PowerShell script. Since I like my scripts to create managed accounts as well, I removed the account that was running my service. Apparently, SharePoint didn't like this and I received an error stating that my <b>SPManagedAccount could not be deleted because other objects depend on it</b> when I loaded the Managed Accounts page in Central Administration. Well, not being able to load the page sort of limits my options for correcting the problem, doesn't it, Microsoft?<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8EU2F71AxMo1_twZYIxYOUNF_Y8CelnpHzX0Cg_dzxH8kWp3MSfhGFthbobDSfohayjnrD_RBeHCDtf64u7Z-nw4E0VmkfJxNJNTLPF2KwQYnNxMNCGEzTXivBeTkmeAzVVr0LKf5HhE/s1600/error.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8EU2F71AxMo1_twZYIxYOUNF_Y8CelnpHzX0Cg_dzxH8kWp3MSfhGFthbobDSfohayjnrD_RBeHCDtf64u7Z-nw4E0VmkfJxNJNTLPF2KwQYnNxMNCGEzTXivBeTkmeAzVVr0LKf5HhE/s400/error.png" width="400" /></a></div><br />
I double checked and the service application had been removed from SharePoint, the Excel Calculation Service had been stopped on the App Server, and the ApplicationPool had even been removed from IIS. Looking up the CorrelationID in the logs also didn't tell me very much.<br />
<br />
I decided to pop open PowerShell and see what I could pull off. Turns out it was a rather simple fix. First, I looked up my Managed Account. I then tried to remove it and found out the dependency was an application pool. After removing the dependency I was about to remove the Managed Account! The PowerShell Commands to do this were:<br />
<ul><li>Get-SPManagedAccount</li>
<li>Get-SPServiceApplicationPool</li>
<li>Remove-SPServiceApplicationPool</li>
<li>Remove-SPManagedAccount</li>
</ul>Here is a screenshot of the PowerShell at work:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl78Elg0iel5ujPCGZ4_9o0ugE6F5_i6cZMbBWND6Ibp0epx4YJZRfoz85aQAfSedJaCY6TrJ5x6MQPuSaN7SZHodiGzRmkrLyEPUV3DMNTZL59UC9U5WK6uJa_0rFa6B05NEVciKr8YM/s1600/ps.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl78Elg0iel5ujPCGZ4_9o0ugE6F5_i6cZMbBWND6Ibp0epx4YJZRfoz85aQAfSedJaCY6TrJ5x6MQPuSaN7SZHodiGzRmkrLyEPUV3DMNTZL59UC9U5WK6uJa_0rFa6B05NEVciKr8YM/s640/ps.png" width="520" /></a></div>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-35841939922243598062010-06-17T11:24:00.002-04:002010-06-17T11:26:34.688-04:00SP2010 Dispose Patterns and Broken WebPartsToday I was building a web part for SharePoint 2010 and ran across an interesting problem. The web part was working fine for authenticated users, but was failing for anonymous users. My first thought was that Lockdown might have been the problem, but it turned out after looking through SharePoint log files that I was running into a dispose problem:<br />
<br />
<blockquote>Trying to use an SPWeb object that has been closed or disposed and is no longer valid.</blockquote><br />
It's an interesting error message, considering that I was disposing of my object, but only after I'd finished with it. I believe the problem wasn't that my code was using a disposed SPWeb object, but rather that I had closed an SPWeb that SharePoint was relying upon.<br />
<br />
Here was the original code, which, according to <a href="http://msdn.microsoft.com/en-us/library/aa973248.aspx">Best Practices</a>, was closing objects that don't need to be closed:<br />
<br />
<pre class="brush: csharp">using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.RootWeb)
{
SPList myList = web.Lists["myList"];
//code here to find a list item
}
}</pre><br />
And the fix was as simple as changing it to this:<br />
<br />
<pre class="brush: csharp">SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;
SPList myList = web.Lists["myList"];
</pre><br />
I'm not so sure this would have broken a SharePont 2007 site, but it's a good reminder to make sure you are following Best Practices for disposing objects!Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-49483189423795218252010-06-03T16:14:00.001-04:002010-06-03T16:33:50.247-04:00SharePoint 2010 Custom Error Messages for Public Facing DeploymentsIt's a fairly common requirement when building a public facing SharePoint site to make sure all pages share branding elements. This includes the SharePoint error pages. While working on a publishing site, you've probably encountered an article that touches on one of the error pages, but for some reason I haven't seen one that tries to cover all of them. So hopefully this will do the job!<br />
<br />
<b>Error Pages</b><br />
<br />
If you are just looking to control the basic error pages, then look no further than the SPWebApplication.SPCustomPage enumeration, which provides a list of commonly updated pages. You use the SPWebApplication.UpdateMappedPage() method to set which pages SharePoint should serve up for each of the values within the enumeration. For a good example of this, see <a href="http://todd-carter.com/post/2010/04/07/An-Expected-Error-Has-Occurred.aspx">this post</a>.<br />
<br />
<b>HTTP 404</b><br />
But what about other HTTP status codes? Well the easiest would be the 404. SharePoint already has support for a custom 404 page, as long as it is pure HTML. The default 404 is located in 14\TEMPLATE\LAYOUTS\1033\sps404.html. The quickest way to override this is to use a feature to deploy your own HTML page into the same folder. A feature receiver can then update the SPWebApplication.FileNotFound property with a relative path to your file. There are just a couple points to keep in mind when you do this:<br />
<ol><li>Your custom page should be greater than 512 bytes. This is because the default feature in Internet Explorer to show "friendly" error messages will sometimes ignore pages smaller than this. You can read more about this problem at <a href="http://support.microsoft.com/kb/218155">Microsoft Support.</a></li>
<li>Your custom page, if encoded as UTF-8, should not have a BOM. Even if you save your file as UTF-8 without BOM, editing it later in Visual Studio will add the BOM back, so be careful. You can read more about that problem <a href="http://andreasglaser.net/post/2009/03/15/SharePoint-and-custom-404-Page-Not-Found-and-UTF-8-issue-with-Firefox.aspx">here.</a></li>
</ol><br />
If you'd like to have a custom 404 that performs server side code, such as to include web parts, you'll need to get a little more creative. There are two common ways to handle this. The first would be to have a static html page that performs a client side redirect with either a META tag or javascript. If you'd like to see that approach in action, <a href="http://sharepointsmart404.codeplex.com/">here is an example</a>.<br />
<br />
A more robust solution would be to <a href="http://blog.mastykarz.nl/accessible-404-pagenotfound-in-microsoft-office-sharepoint-server-2007/">use an HttpModule</a>. With the HttpModule, you have two more possibilities for how you serve your page. You can use a Response.Redirect, which is what the linked article suggests. The only downside to this is that the URL changes, which may or may not be desired. Alternatively, to keep the URL, you would need to use Response.Write. <br />
<br />
<b>HTTP 401</b><br />
Now that you have custom error pages for the SPCustomPage enumered pages as well as a 404 page. But what about a 401? The simplest way you might think to do this would be to edit the web.config CustomErrors element. In any normal ASP.NET application, that would have been sufficient. Unfortunately it doesn't work in SharePoint. <br />
<br />
Your next instinct might be to try IIS, as it provides the ability to set custom error pages. If you edit the property for the 401.2 status code and point it to a custom HTML page on your site, it may or may not work. In testing, it turns out that having Anonymous Access enabled in SharePoint (which then sets it in IIS) prevents a custom 401.2 page from being used. However, if Anonymous Access is disabled, such as with an Intranet site, then the custom page will show just fine. Since we are talking about a public facing deployment you are probably using the Publishing template, so you're going to need a different approach.<br />
<br />
The trick for a custom 401 message when you have Anonymous Access enabled is using a custom http module, much like you could have done for the 404. <br />
<br />
Here is an example:<br />
<br />
<pre class="brush:csharp">public class CustomPageMappingsHttpModule : IHttpModule
{
private const string Custom401File = "/_layouts/1033/Custom401_CSS.htm";
private HttpApplication _application;
public void Dispose()
{
}
public void Init(HttpApplication context)
{
this._application = context;
this._application.PreSendRequestHeaders += new EventHandler(_application_PreSendRequestHeaders);
}
protected void _application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpResponse response = this._application.Response;
if (response.ContentType.Equals("text/html", StringComparison.CurrentCultureIgnoreCase))
{
string message = string.Empty;
switch (response.StatusCode)
{
case 401:
message = File.ReadAllText(this._application.Server.MapPath(Custom401File));
this._application.Response.Clear();
this._application.Response.Write(message);
break;
}
}
}
}
</pre><br />
Once you hook that up in your web.config, go ahead and try to load http://YourSite/_layouts/settings.aspx. You should get the same authentication prompt you'd expect for an anonymous user, but if you hit cancel you'll now see your custom 401 message. Note that the way you register an HttpModule in 2010 is slightly different. If you add the module to the httpModules element in web.config, it may not work. In testing, I was only able to get my module to work by adding it to the modules element with the other SharePoint HttpModules.<br />
<br />
As a final level of customization, you could consider having no prompt at all on your public site when users request restricted content. The way to do this is to extend the site and have a private site as well. Since we are just talking about two different web sites in IIS (with corresponding settings and web.config files), you can control their authentication and error settings independently while still serving up the same content. The public site could then have Anonymous Access enabled and Windows Authentication disabled. This removes the login prompt when a 401 occurs on the public site. The private site would have Anonymous Access disabled but Windows Authentication enabled, allowing users to log in and manage content as needed. <br />
<br />
<b>Quirks</b><br />
Since you are probably using the Publishing template, it's worth mentioning the Lockdown feature. This feature stops users from being able to see your Form pages. It also locks down lists and libraries, such as the Style Library. So make sure if you use CSS in your custom error pages that you know whether you have lockdown enabled and plan locations for your assets appropriately.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com3tag:blogger.com,1999:blog-6363288400368237015.post-45211354842119872512010-05-26T15:56:00.007-04:002010-08-10T13:12:54.617-04:00SharePoint 2010 Content Deployment Jobs Missing Deployment Options SectionI've been working with Content Deployment jobs lately, and noticed that in 2010, the section on the Create Jobs page where you can specify whether to perform an incremental or full content deployment was missing. Just to make sure I wasn't crazy, I looked up the old screen from 2007, which looks like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEN_ic-spr36bZdeiBxRfL59PFTtevfjooENiBaK98Cdybo5x1nCvzvL5xmuZWzHSYU8HdiB84FuIExm5PNr2BzI271ft7ebkZW70DiTp59k9BxIRl9g3n4A2ObMD-P_XhlIB0zZmtCq4/s1600/cd2007.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEN_ic-spr36bZdeiBxRfL59PFTtevfjooENiBaK98Cdybo5x1nCvzvL5xmuZWzHSYU8HdiB84FuIExm5PNr2BzI271ft7ebkZW70DiTp59k9BxIRl9g3n4A2ObMD-P_XhlIB0zZmtCq4/s400/cd2007.png" width="397" /></a></div><br />
As you can see, there is a section there for setting the types of content that are deployed. The first option, "Deploy only new, changed, or deleted content" is the incremental deployment. The second option, "Deploy all content, including content that has been deployed before", is the full deployment.<br />
<br />
Now compare that screen to the 2010 equivalent:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3JXw0AV3G0STewLBp4Z-7K2c8TLXX6aKb3qOEn6WUG2E3Sp01lcAhJHwzPDTtnSyRLgmppMrZ1KEocw7IybJXltS6NkJZtUr182lQUP0c7KSVzEkP_OtYh9DCIVn_BwhL1VPGnP8oUxI/s1600/cd.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3JXw0AV3G0STewLBp4Z-7K2c8TLXX6aKb3qOEn6WUG2E3Sp01lcAhJHwzPDTtnSyRLgmppMrZ1KEocw7IybJXltS6NkJZtUr182lQUP0c7KSVzEkP_OtYh9DCIVn_BwhL1VPGnP8oUxI/s400/cd.png" width="365" /></a></div>For some reason, the Deployment Options section is missing. This is not gonna work. If you recall from 2007, doing a full deployment after the initial deployment can cause problems. See <a href="http://blogs.technet.com/b/stefan_gossner/archive/2009/10/30/content-deployment-the-complete-guide-part-1-the-basics.aspx">this article</a> for more information about why.<br />
<br />
<br />
So, how do we create an incremental job? The answer, courtesy of <a href="http://blog.beckybertram.com/default.aspx">Becky Bertram, MVP</a>:<br />
<blockquote>You can't do it through Central Administration, but you can specify whether you want to do a full or incremental deployment using PowerShell. Go to your SharePoint PowerShell prompt and type get-help new-spcontentdeploymentjob -detailed and then take a look at the IncrementalEnabled parameter.</blockquote><br />
Here is a simple script to create an incremental content deployment job, assuming you have already defined a content deployment path of "Authoring to Production":<br />
<br />
<pre class="brush:csharp;">New-SPContentDeploymentJob -Name "Authoring to Production - Incremental"
-SPContentDeploymentPath "Authoring to Production" -IncrementalEnabled:$true
</pre><br />
Looking in Central Administration, you'll see your created job, but you still can't see if it's incremental or full. To do that, run <b>Get-SPContentDeploymentJob "Authoring to Production - Incremental"</b>. You should notice the following line in your output:<br />
<ul><li>ExportMethodType : ExportChanges<br />
</li>
<li></li>
</ul>That sounds awfully like an incremental doesn't it? Let's try setting the IncrementalEnabled parameter to false and creating another job: <br />
<pre class="brush:csharp;">New-SPContentDeploymentJob -Name "Authoring to Production - Full"
-SPContentDeploymentPath "Authoring to Production" -IncrementalEnabled:$false
</pre>Now run <b>Get-SPContentDeploymentJob "Authoring to Production - Full"</b> and you should see a slightly different export method type: <br />
<ul><li>ExportMethodType : ExportAll</li>
</ul>So, this means that your Content Deployment job is going to be incremental by default. The only way to get a full job is to create it via PowerShell. Given the limitations and caveats with full jobs, this seems like a good change.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-91240532365383384012010-05-06T10:06:00.002-04:002010-05-06T10:07:39.055-04:00SP 2010 - Web Parts and Embedded Resources Not WorkingI ran into a problem recently trying to use Embedded Resources with SharePoint 2010 web parts. I was using code I had directly used in non SharePoint ASP.Net server controls, which if you aren't familiar with, is pretty simple. I'll briefly describe how you'd use an embedded resource, then describe my problem and how I fixed it.<br />
<br />
First, you need to just add your resource, which in my case was a stylesheet, to your project. Then, using the properties pane, set the Build Action to Embedded Resource. Once you've done that, you'll need to edit the AssemblyInfo.cs file within your project and enable the embedded resource.<br />
<br />
<pre class="brush: csharp">[assembly: WebResource("MyAssembly.styles.MyStyleSheet.css", "text/css")]
</pre><br />
Now you have a resource that will be embedded in your DLL. The path to that resource is specified in the WebResourceAttribute above. I usually compile and open my DLL in reflector at this point to confirm that the path I've picked is correct. Once it is, you can include your stylesheet like this:<br />
<br />
<pre class="brush: csharp">string styleSheetUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyAssembly.styles.MyStyleSheet.css");
LiteralControl styleSheetLink = new LiteralControl(string.Format("LINK SYNTAX HERE - BLOGGER WONT LET ME EMBED THE HTML", styleSheetUrl));
Page.Header.Controls.Add(styleSheetLink);
</pre><br />
In theory, that should work, right? It works in server controls, and it's worked for me in older web parts for MOSS 2007. But, when I built my 2010 web part, it wasn't working.<br />
<br />
I viewed source on my page and could see that the embedded resource was added to the markup. But when I tried to load that URL myself, I got an error, implying my resource wasn't there at all! I checked and rechecked my path inside the DLL. Everything was right.<br />
<br />
But then I noticed something different about 2010 web parts. They use a LoadControl mechanism. I was accessing my embedded resources, using the above code, directly inside the webpart.ascx.cs, not the webpart.cs file. On a hunch, I moved the code into the webpart.cs file. Success!<br />
<br />
This got me curious, so I looked at the source for the generated HTML and noticed that the generated URL for the embedded resource had changed. Apparently, the path is based somewhat on that first parameter to GetWebResourceUrl(), which is a type. Tinkering a little more, I learned that I could actually leverage the embedded resource from the webpart.ascx.cs by making a minor adjustment.<br />
<br />
<pre class="brush: csharp">string styleSheetUrl = Page.ClientScript.GetWebResourceUrl(this.Parent.GetType(), "MyAssembly.styles.MyStyleSheet.css");
</pre><br />
Notice that I'm not passing the type of the parent, which for our webpart.ascx.cs, is the wrapper class that calls LoadControl.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-58586621035072482192010-05-04T12:09:00.003-04:002010-05-04T12:14:12.793-04:00SP 2010 - LINQ versus CAML Joins and the Nuances of Projected FieldsWhen working with relational lists in SharePoint 2010, you have the option to use LINQ to SharePoint or CAML to join those lists to pull data out. While LINQ is easier to use and will leverage CAML under the covers, it is not always capable of performing queries that CAML can directly.<br />
<br />
For instance. Let's say you have a list, called Parent. This list has a single valued lookup column, PrimaryChild, that refers to the Children list. LINQ can very easily perform a query with a where clause based on PrimaryChild:<br />
<br />
<pre class="brush: csharp;">var parents = from p in context.Parents
where p.PrimaryChild.Title == "My First Child"
select p;
</pre><br />
However, if you were to create a second lookup column, OtherChildren, that allowed multiple values, LINQ would run into difficulties because the lookup is now represented as an EntitySet. With a single valued lookup field, you would instead have just a strongly typed object, with direct access to the fields within that list item.<br />
<br />
<pre class="brush: csharp;">var parents = from p in context.Parents
where p.OtherChildren.Any(c => c.Title == "My Other Child")
select p;
</pre><br />
Running this query will throw an exception that semi-efficient queries are not allowed. If you recall, this is because LINQ leverages <a href="http://msdn.microsoft.com/en-us/library/ee536585%28office.14%29.aspx">Two-Stage Queries</a>. So, you can continue with LINQ and perform the query in two stages, or you can change the query to use CAML.<br />
<br />
Here is the same query in CAML, but this time, the query will not cause an exception.<br />
<br />
<pre class="brush: csharp;">using (SPSite site = new SPSite(SPContext.Current.Site))
{
SPWeb web = site.RootWeb;
if (web != null)
{
SPList list = web.Lists["Parents"];
if (list != null)
{
SPQuery query = new SPQuery();
StringBuilder sbQuery = new StringBuilder();
sbQuery.Append("<where><eq>");
sbQuery.Append("<fieldref name="\"OtherChildren\"">");
sbQuery.Append("<value type="\"LookupMulti\"">My Other Child</value>");
sbQuery.Append("</fieldref></eq>");
query.Query = sbQuery.ToString();
StringBuilder sbJoins = new StringBuilder();
sbJoins.Append("<join listalias="\"OtherChildren\"" type="\"LEFT\"">");
sbJoins.Append("<eq>");
sbJoins.Append("<fieldref name="\"OtherChildren\"" reftype="\"ID\"">");
sbJoins.Append("<fieldref list="\"OtherChildren\"" name="\"ID\"">");
sbJoins.Append("</fieldref> ");
sbJoins.Append("</fieldref> ");
query.Joins = sbJoins.ToString();
StringBuilder sbProj = new StringBuilder();
sbProj.Append("<field list="\"OtherChildren\"" name="\"OtherChildrenNickname\"" showfield="\"Nickname\"" type="\"Lookup\"">");
query.ProjectedFields = sbProj.ToString();
StringBuilder sbView = new StringBuilder();
sbView.Append("<fieldref name="\"Title\"">");
sbView.Append("<fieldref name="\"OtherChildrenNickname\"">");
query.ViewFields = sbView.ToString();
if (!string.IsNullOrEmpty(query.Query))
{
SPListItemCollection matches = list.GetItems(query);
foreach (SPListItem match in matches)
{
Console.WriteLine(match["Title"]);
string rawNickname = (string)match["OtherChildrenNickname"];
if (!string.IsNullOrEmpty(rawNickname))
{
SPFieldLookupValue nickname = new SPFieldLookupValue(rawNickname);
Console.WriteLine(nickname.LookupValue);
}
}
}
}
}
}
</fieldref></fieldref></field></eq></join></where></pre><br />
You'll notice that in the example above I'm performing a CAML join and also leveraging Projected Fields. Here are some guidelines/rules to keep in mind:<br />
<ul><li>The CAML has several attributes that ask for the list name or list alias. This is NOT the actual name of the list. Rather, it is the internal name of the lookup field within your list. So in our example, the list was named Children, and the field was OtherChildren. We used OtherChildren to build the join and projected fields.</li>
<li>Projected Fields used in your query do not have to match up to the Projected Fields you have specified in the Child list. Those are a UI convenience and not used by your CAML.</li>
<li>If you want to display a value from the Child list, you need to make sure you have a Projected Field in your CAML. Only those fields which are projected are eligible to be View fields.</li>
<li>All Projected Fields will become SPFieldLookupValue objects (or perhaps SPFieldLookupValueCollection, though I haven't yet had one of my lists do this). Those lookups will always contain the ID of the child list item, but the value of the selected field within that child item.<br />
</li>
</ul>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-60762821243373512852010-04-29T11:29:00.000-04:002010-04-29T11:29:01.382-04:00SP 2010 - Phonetic Search Only for People Scope?I have a custom scope I've created that I am querying against with the FullTextSqlQuery object. Recently I was asked if we could allow a phonetic search. With all the hype around this new feature in SharePoint 2010, I thought it would be very easy.<br />
<br />
I looked, and sure enough, there is an EnablePhonetic property right on FullTextSqlQuery. I set it to true, ran my search and got back...nothing. I figured maybe it didn't like my query, which had a LIKES keyword. That wasn't it. I tried looking on the web and it seems all the mentions of phonetic search seem to be closer to press releases than coding snippets.<br />
<br />
Finally I found <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.query.enablephonetic%28v=office.14%29.aspx">this nugget</a> on MSDN:<br />
<blockquote>For FAST Search Server 2010 for SharePoint, this property is only applicable for People Search. </blockquote>Well, I'm not using FAST, but I am using a custom search scope. It would appear that might be the limiting factor here. While I am searching for people, they are stored in a custom list, since they are not members of my SharePoint site. Therefore I was using a custom scope to get to them.<br />
<br />
It would seem phonetic search is not available yet to customize in this way.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-74258729960650278212010-04-28T12:01:00.003-04:002010-04-28T12:30:14.866-04:00SP 2010 - Configure and Use a TaxonomyWebTaggingControlIf you are using the Managed Metadata Service and developing a custom web part, you may be interested in using that nice term picker that you see when you create a new list item that contains a Managed Metadata Column.<br />
<br />
First, let's cover the bare minimum code you'll need to get the term picker to show up in your web part. First you'll want to add a reference to Microsoft.SharePoint.Taxonomy to your project. Then you'll need a Register directive in your ascx, like this:<br />
<br />
<pre class="brush: csharp"><%@ Register Tagprefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
</pre><br />
Now you can add the picker to your web part just like any other control.<br />
<br />
<pre class="brush: csharp"><taxonomy:taxonomywebtaggingcontrol id="twtcMyTermSet" runat="server">
</taxonomy:taxonomywebtaggingcontrol></pre><br />
You'll notice that I didn't set any properties on the TaxonomyWebTaggingControl. Well there are three properties you'll need to set, but we'll do it programatically. Those properties are:<br />
<ul><li> SSPList - This property parses your string and builds a List<guid> to set the SspId property</guid></li>
<li>GroupId - A Guid</li>
<li>TermSetList - This property parses your string and builds a List<guid> to set the TermSetId property</guid></li>
</ul><br />
Now, you can set these properties declaratively if you wish, but it won't be a very portable web part, as the Guids aren't part of the import CSV format for the Managed Metadata Service, nor do any of the Taxonomy Create methods allow setting of Ids. Because of this, it's best to have your web part set these properties on the TaxonomyWebTaggingControl programatically. If you just want to see how you would go about setting these properties declaratively, scroll to the bottom of this post, as I've included it for reference.<br />
<br />
In order to make your web part portable, you're going to need to query the Managed Metadata Service and get these values yourself. Moreover, you're going to have to do it on every load of your web part since these properties are not stored anywhere between page loads.<br />
<br />
<b>Note</b>: <i>If you only set these properties on page load, this will work as long as there are no scenarios where you need to redraw the control again, such as having a custom server validator. In that instance, your picker would work before the postback, but the look-ahead feature would be broken after the postback.</i><br />
<br />
In order to make loading our properties a little easier, here is an extension method for TaxonomyWebTaggingControl that allows you to quickly set the properties. The path part of this was <a href="http://pholpar.wordpress.com/2010/02/10/getting-term-by-the-path-in-the-taxonomy-hierarchy/">inspired by PHolpar</a>. Also, remember that extension methods must be defined in static classes.<br />
<br />
<pre class="brush: csharp">public static bool Configure(this TaxonomyWebTaggingControl twtc, string termSetPath)
{
bool configured = false;
TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
string[] parts = termSetPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0)
{
TermStore termStore = GetTermStore(session, parts[0]);
if (termStore != null && parts.Length > 1)
{
twtc.SSPList = termStore.Id.ToString();
Group group = GetGroup(termStore, parts[1]);
if (group != null && parts.Length > 2)
{
twtc.GroupId = group.Id;
TermSet termSet = GetTermSet(group, parts[2]);
if (termSet != null)
{
twtc.TermSetList = termSet.Id.ToString();
configured = true;
}
}
}
}
return configured;
}
</pre><br />
In order to load MyTermSet, which lives within MyGroup, which resides within MyTermStore, you will use this extension method similar to the following: <br />
<br />
<pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e)
{
bool configured = twtcSpecialty.Configure("MyTermStore/MyGroup/MyTermSet");
if (!configured)
{
throw new ApplicationException("Unable to find target TermSet");
}
}
</pre><br />
The result looks just like the picker from other parts of SharePoint, with functioning look-ahead.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnOrtJ4CdM171Kpizpj6ar9zH0EmpmHnin8oRhzHAIFjN22U4CNQYS0vFNhI6AjpPClB1P3ufu-lcnWVxYCuwnhsSsr2wzaGMUiDnEKvm2K5yjzqqZC_Vi806Ka2-k4tZ5AhY7O3FKcHU/s1600/termpicker.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnOrtJ4CdM171Kpizpj6ar9zH0EmpmHnin8oRhzHAIFjN22U4CNQYS0vFNhI6AjpPClB1P3ufu-lcnWVxYCuwnhsSsr2wzaGMUiDnEKvm2K5yjzqqZC_Vi806Ka2-k4tZ5AhY7O3FKcHU/s1600/termpicker.png" /></a></div><br />
As promised, here is the markup if you want to configure your TaxonomyWebTaggingControl declaratively.<br />
<br />
<pre class="brush: csharp"><taxonomy:taxonomywebtaggingcontrol groupid="2f5fcd2c-0925-4a9a-bd56-3fac19f85f53" id="twtcMyTermSet" runat="server">
<ssplist>0d8382cf-2d63-4421-a37a-b9386d0be5c8</ssplist>
<termsetlist>7adeced1-b73e-47dc-b695-82bb28e2f48f</termsetlist>
</taxonomy:taxonomywebtaggingcontrol>
</pre>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com1tag:blogger.com,1999:blog-6363288400368237015.post-30704660508978262362010-04-27T10:55:00.000-04:002010-04-27T10:55:21.363-04:00SP 2010 - Randomize the order of your search results from FullTextSqlQueryI had a need recently to perform a query with FullTextSqlQuery but to limit the results and then display them in a random order. The FullTextSqlQuery class does support a RowLimit property, though this would not work in my case because I didn't want to get the exact same answers everytime.<br />
<br />
Remembering that <a href="http://msdn.microsoft.com/en-us/library/ee536585%28office.14%29.aspx">LINQ to SharePoint often requires Two-Stage Queries</a>, I decided to employ a similar approach here and pull back the data I could then perform more culling server side.<br />
<br />
<pre class="brush: csharp">DataTable searchResults = PerformSearch(query, queryRowLimit);
if (searchResults != null && searchResults.Rows.Count > 0)
{
int limit = 10; //todo: this should be a webpart property
var results = (from row in searchResults.AsEnumerable()
orderby Guid.NewGuid()
select row).Take(limit);
}
</pre><br />
The PerformSearch method is just a standard setup for using FullTextSqlQuery to return a DataTable. Note that I limit my resultset as much as I can with the FullTextSqlQuery.QueryText property to try to avoid hitting a throttling exception.<br />
<br />
With the limited results returned, I then want to randomize the order of the records. LINQ allows us to do this the same way we would in SQL, and I just order by a random Guid. Once the results are randomized, we just grab the number of records we need with the Take() extension method.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-57489273086813486602010-04-23T14:52:00.003-04:002010-04-23T14:54:15.347-04:00SP 2010 Managed Metadata TermSet.CreateTerm Throws ErrorI've been working on importing a set of Terms into my Managed Metadata Term Store from a 3rd party database. However, I ran into a snag. When I execute the following code, I get an error, "<b>There is already a term with the same default label and parent term.</b>"<br />
<br />
<pre class="brush: csharp">public static void CreateTermIfNotExists(TermSet termSet, string termName)
{
if (termSet != null && !string.IsNullOrEmpty(termName))
{
Term term = null;
//This throws an exception if the Term doesn't exist
try
{
term = termSet.Terms[termName];
}
catch { }
if (term == null)
{
Term t = termSet.CreateTerm(termName, 1033);
termSet.TermStore.CommitAll();
}
}
}
</pre><br />
I attached my debugger and found out that even though I was checking for my term, the code would say it wasn't there, even though it was! The problem, was that the specific term causing me problems had an ampersand. The term name I supplied was "Foo & Bar", but the value put into the TermStore actually contained unicode version of the ampersand.<br />
<br />
Looking through the <a href="http://msdn.microsoft.com/en-us/library/ee577519%28v=office.14%29.aspx">documentation</a>, I found this relevant comment:<br />
<br />
<blockquote>The name value will be normailized to trim consecutive spaces into one and replace the & character with the wide character version of the character (\uFF06). The leading and trailing spaces will be trimmed. It must be non-empty and cannot exceed 255 characters, and cannot contain any of the following characters ; "<>|&tab. </blockquote><br />
That was indeed the behavior I was seeing. Thinking I had just found a limitation of the TermSet.Terms collection, I changed my code to this:<br />
<br />
<pre class="brush: csharp">public static void CreateTermIfNotExists(TermSet termSet, string termName)
{
if (termSet != null && !string.IsNullOrEmpty(termName))
{
Term term = null;
TermCollection tc = termSet.GetTerms(termName, 1033, true, StringMatchOption.ExactMatch, 1, false);
if (tc != null && tc.Count > 0)
{
term = tc[0];
}
if (term == null)
{
Term t = termSet.CreateTerm(termName, 1033);
termSet.TermStore.CommitAll();
}
}
}
</pre><br />
I ran again, and this time instead of blowing up on just that one case, my code went crazy trying to insert a bunch of different terms that were working fine before and throwing way more exceptions. A little research on this method led me to <a href="http://stackoverflow.com/questions/2520360/sharepoint-2010-managed-metadata-unable-to-get-term-from-termset">this post</a>, which suggests the TermSet.GetTerms method does not work in Beta 2, which seems to be what I just discovered as well.<br />
<br />
I decided to explore the reference to normalizing term names from the MSDN link. My final pass at the code became:<br />
<br />
<pre class="brush: csharp">public static void CreateTermIfNotExists(TermSet termSet, string termName)
{
if (termSet != null && !string.IsNullOrEmpty(termName))
{
Term term = null;
try
{
string normalizedTermName = TermSet.NormalizeName(termName);
term = termSet.Terms[normalizedTermName];
}
catch { }
if (term == null)
{
Term t = termSet.CreateTerm(termName, 1033);
termSet.TermStore.CommitAll();
}
}
}
</pre><br />
Finally, success! Moral of the story - when you query your TermSet for a specific Term you need to normalize your name first, because that is how it will be stored internally.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com2tag:blogger.com,1999:blog-6363288400368237015.post-62648678859105949432010-04-19T11:34:00.003-04:002010-04-19T11:36:38.875-04:00SP 2010 - Using LINQ to SharePoint to Find List Items with Specific Managed Metadata TermsNow that I can effectively use LINQ to access my Managed Metadata Columns, I'd like to only pull back those columns that contain values I need. For single valued Managed Metadata Columns, this is very straightforward:<br />
<br />
<pre class="brush: csharp">var examples = from d in context.Examples
where d.Specialty is TaxonomyFieldValue && ((TaxonomyFieldValue)d.Specialty).Label == "Value One"
select d;
</pre><br />
For multi valued Managed Metadata Columns, my first attempt was a bust. I tried the following expression, but received a compiler error, "<b>An expression tree may not contain an anonymous method expression</b>".<br />
<br />
<pre class="brush: csharp">var examples = from d in context.Examples
where ((TaxonomyFieldValueCollection)d.Specialty).Exists(delegate(TaxonomyFieldValue tfv){
return tfv.Label == "Value One";
}) != null
select d;
</pre><br />
The hint here from the compiler is that I can do this as long as I don't use an anonymous method. So I created a method to test for the term I wanted and changed my expression to call this method.<br />
<br />
<pre class="brush: csharp">private static bool ContainsMetadataTerm(object o, string termLabel)
{
bool exists = false;
if (o is TaxonomyFieldValueCollection)
{
TaxonomyFieldValueCollection tfvc = (TaxonomyFieldValueCollection)o;
exists = tfvc.Exists(delegate(TaxonomyFieldValue tfv)
{
return tfv.Label == termLabel;
});
}
return exists;
}
var examples = from d in context.Examples
where d.Specialty is TaxonomyFieldValueCollection && ContainsMetadataTerm(d.Specialty, "Value One")
select d;
</pre><br />
Now I can query specifically for list items that use specific terms. Keep in mind that this filtering does not happen until after the list items have been pulled down, so you may want to do some additional filtering to narrow the results so you don't run afoul of the new Throttling feature of SharePoint 2010.<br />
<br />
Here was the CAML generated by the last LINQ query, which shows that the additional filtering for Term was done after the records were pulled.<br />
<br />
<pre class="brush: xml"><view>
<query>
<where>
<beginswith>
<fieldref Name="ContentTypeId" />
<value Type="ContentTypeId">0x0100</Value>
</BeginsWith>
</Where>
</Query>
<ViewFields="">
<fieldref Name="First_x0020_Name" />
<fieldref Name="Specialty" />
<fieldref Name="ExampleId" />
<fieldref Name="ID" />
<fieldref Name="owshiddenversion" />
<fieldref Name="FileDirRef" />
<fieldref Name="Title" />
</ViewFields>
<rowlimit Paged="TRUE">2147483647</RowLimit>
</View>
</pre>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-43436125234619280882010-04-19T10:59:00.005-04:002010-04-19T11:03:03.057-04:00SP 2010 - Managed Metadata Columns ARE Supported in LINQ to SharePointI apparently <a href="http://sharepointlearningcurve.blogspot.com/2010/04/sp-2010-managed-metadata-columns-not.html">spoke too soon</a>! You can get to Managed Metadata Columns in LINQ to SharePoint with SPMetal, just not directly off the command line. You need to supply a parameters option.<br />
<br />
First you'll want to create your parameters file. I've found two different ways to get the Managed Metadata Column to show up. The first attempt I used this XML in my parameter file.<br />
<br />
<pre class="brush: xml"><web xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
<list name="Examples">
<contenttype class="Example" name="Item">
<includehiddencolumns>
</includehiddencolumns>
</contenttype>
</list>
</web>
</pre><br />
I saved this file to <b>metaloptions.xml</b> and then ran the following command.<br />
<br />
<b>SPMetal.exe /web:http://mysite /code:SPMySite.cs /namespace:SPMySite /parameters:metaloptions.xml</b><br />
<br />
The output of that command will include a warning, but it seems to have no impact on LINQ working. I think it's purely informational and not relevant to what we are working with.<br />
<br />
<blockquote>Warning: All content types for list Form Templates were excluded.</blockquote><br />
Now when I query with LINQ I see the hidden fields that power my Managed Metadata Column, which <a href="http://sharepointlearningcurve.blogspot.com/2010/04/sharepoint-2010-site-columns-for.html">look familiar</a>.<br />
<br />
On my typed list item object, I now had three new fields:<br />
<ul><li>Specialty_0 - String</li>
<li>TaxonomyCatchAllColumnCatchAllData - IList<string></string></li>
<li>TaxonomyCatchAllColumnId - IList<int?></int?></li>
</ul><br />
Those fields hold values that look like this, respectively. Unfortunately, this isn't terribly useful.<br />
<ul><li>Value One|e203149a-6852-46fb-9d8e-9c21d350068d</li>
<li>z4KDDWMtIUSjerk4bQvlyA==|0c7eej633Ee2lYK7KOL0jw==|mhQD4lJo+0adjpwh01AGjQ==</li>
<li>4</li>
</ul><br />
I tried another pass on my parameters file. This time I used the following XML. <br />
<br />
<pre class="brush: xml"><?xml version="1.0" encoding="utf-8"?>
<web xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
<list Name="Examples">
<contenttype Name="Item" Class="Example">
<column Name="Specialty" />
</ContentType>
</List>
</Web>
</pre><br />
On my typed list item object, I now have the one extra Column that I specified in my parameters file. LINQ will create a property for this field that is just an object. However, if you access this property, you can cast it to a TaxonomyFieldValueCollection or TaxonomyFieldValue, as appropriate. This provides exactly the information we wanted:<br />
<br />
<pre class="brush: csharp">using (SPMySiteDataContext context = new SPMySiteDataContext("http://mysite"))
{
var examples = from d in context.examples
select d;
foreach (var example in examples)
{
if (example.Specialty is TaxonomyFieldValueCollection)
{
foreach (TaxonomyFieldValue tfv in (TaxonomyFieldValueCollection)example.Specialty)
{
Console.WriteLine(tfv.Label);
}
}
else if (example.Specialty is TaxonomyFieldValue)
{
TaxonomyFieldValue tfv = (TaxonomyFieldValue)example.Specialty;
Console.WriteLine(tfv.Label);
}
}
}
</pre>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com2tag:blogger.com,1999:blog-6363288400368237015.post-72103366842897007632010-04-19T10:12:00.001-04:002010-04-19T11:04:11.915-04:00SP 2010 - Managed Metadata Columns Not Supported in LINQ to SharePoint?Today I am playing with LINQ to SharePoint. I created a simple list and added a Managed Metadata Column to my list. I then used SPMetal to generate a DataContext class. My Managed Metadata Column is nowhere to be found. I looked through options for SPMetal to see if maybe it just needed a switch to capture those Managed Metadata Columns, but I don't see one.<br />
<br />
So it appears that Managed Metadata Columns have no support in LINQ to SharePoint. They are also not eligible to be Projected Fields. It would seem Microsoft didn't really flesh out all the ways the new Managed Metadata Service might be used.<br />
<br />
<b>Update: <a href="http://sharepointlearningcurve.blogspot.com/2010/04/sp-2010-managed-metadata-columns-are.html">I figured out how to do this</a>.</b>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-18392074040325878882010-04-15T16:08:00.001-04:002010-04-15T16:11:27.406-04:00SP 2010 - Creating ListItems with Managed Metadata ColumnsToday I needed to create some list items programatically for a list that contained Managed Metadata columns. Since Managed Metadata columns are specialized SPLookups under the covers, it's a little more difficult to set your field value than just assigning the text value of your Term.<br />
<br />
Here is a method that will set the value for you, and an example of how to use it. Note that in my example the Managed Metadata column is multi-value, even though the example method is only set up to add a single Term. If you have a single-value Managed Metadata column, you can leave out the <b>TaxonomyFieldValueCollection</b>.<br />
<br />
<pre class="brush: csharp">class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://mysite"))
{
SPList pl = site.RootWeb.Lists["MyList"];
if (pl != null)
{
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["Managed Metadata Service"];
Group group = termStore.Groups["MyGroup"];
TermSet termSet = group.TermSets["MyTermSet"];
SPListItem li = pl.AddItem();
li[SPBuiltInFieldId.Title] = "my new item";
Term term = termSet.Terms["MyTerm"];
UpdateListItemTermColumn(li, "MyMetadataField", term);
li.Update();
}
}
}
static void UpdateListItemTermColumn(SPListItem li, string fieldName, Term term)
{
SPField termField = li.Fields[fieldName];
TaxonomyFieldValueCollection tfvc = new TaxonomyFieldValueCollection(termField);
tfvc.Add( new TaxonomyFieldValue(termField));
li[fieldName] = tfvc;
TaxonomyField taxonomyField = termField as TaxonomyField;
if (taxonomyField != null)
{
taxonomyField.SetFieldValue(li, term);
}
}
}
</pre>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-35554125761464372062010-04-15T11:33:00.000-04:002010-04-15T11:33:39.651-04:00SP 2010 - Empty Logs Versus Least Permissions InstallEarlier this week I was having <a href="http://sharepointlearningcurve.blogspot.com/2010/04/sharepoint-2010-uls-problems-logs-are.html">problems with my SharePoint logs being empty</a>. At the time, the only fix I could discover was giving my AppPool identity Administrator rights on the machine. Obviously the reason the account lacked such permissions was because I had given it no rights when I created it - the same way I would have when I install MOSS 2007.<br />
<br />
Well giving Administrator rights to your AppPool identities in SharePoint 2010 is also a bad thing, as shown by the new Health Monitoring:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk40KZtv7Mfk2WjOvlk6oDBsNN6lu_nG-HcMF89hRYJGeifCCNIyj02zxAkQJFiv3XeluqTQx44udFejB-FrB_1P-tRjzdqJY7RF1iO2RCZjJcA8Sa7WHiBhAt4aCPM_o7Np3E3lvTYUA/s1600/error.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="48" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk40KZtv7Mfk2WjOvlk6oDBsNN6lu_nG-HcMF89hRYJGeifCCNIyj02zxAkQJFiv3XeluqTQx44udFejB-FrB_1P-tRjzdqJY7RF1iO2RCZjJcA8Sa7WHiBhAt4aCPM_o7Np3E3lvTYUA/s640/error.png" width="640" /></a></div><br />
However, at this time, I can't seem to find what permission set is needed for ULS to be accessible by my AppPool account. Right now my choices are to upset Health Monitoring or to have logs. Having both is apparently a luxury.Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0tag:blogger.com,1999:blog-6363288400368237015.post-29565310635228134792010-04-14T14:06:00.007-04:002010-04-14T14:13:32.612-04:00SharePoint 2010 - Can't Open Crawled PropertiesI have been working on building some custom Search components, which will leverage my own Managed Properties. However, early on in the process I hit a snag.<br />
<br />
I created some sample content and then running a full index of my farm. SharePoint was able to discover some new Crawled Properties during this process, which I was hoping to turn into Managed Properies. However, when clicking on any of the new Crawled Properties, I get an error <b>"Unable to cast object of type 'System.DBNull' to type 'System.String'"</b>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9R-3lVoKWQmuQ_-rfqBQqU97rXxtc_DjTS3-ObOemxTMBRxdaCzcyhQUMu1ZN2B4rjyyemJiMmdejsQF0eWoJ6N0UZYmsgPeittZwC3KsinxE0IqMmRp0qcsH6LqQgbcBtmff5la0uHM/s1600/error_cropped.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9R-3lVoKWQmuQ_-rfqBQqU97rXxtc_DjTS3-ObOemxTMBRxdaCzcyhQUMu1ZN2B4rjyyemJiMmdejsQF0eWoJ6N0UZYmsgPeittZwC3KsinxE0IqMmRp0qcsH6LqQgbcBtmff5la0uHM/s400/error_cropped.png" width="400" /></a></div><br />
<br />
Using my <a href="http://sharepointlearningcurve.blogspot.com/2010/04/sharepoint-2010-uls-problems-logs-are.html">newly working SharePoint logs</a>, I found the following error message:<br />
<br />
<div style="margin-left:25px;"><b>SchemaDatabase.GetSamples:Error occurred when reading [SampleUrl] System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'. at Microsoft.Office.Server.Search.Administration.SchemaDatabase.GetSamples(CrawledProperty crawledProperty, Int32 sampleCount)</b><br />
</div><br />
I then opened up Reflector to find what was going on in that method. It turns out to be a very simple method that just calls a stored procedure. I fired up SQL Server Profiler and tracked down this call, which ultimately was breaking the page:<br />
<br />
<div style="margin-left:25px;"><b>exec dbo.proc_MSS_GetCrawledPropertySamplesByPropertyID @CrawledPropertyId=334,@SampleCount=5</b><br />
</div><br />
So, as it turns out, this SProc does handle NULLs, just not as robustly as we might want! Here is the SProc, which I found in the <b>Search_Service_Application_PropertyStoreDB_{GUID}</b> database:<br />
<br />
<pre class="brush: sql">CREATE PROCEDURE dbo.proc_MSS_GetCrawledPropertySamplesByPropertyID
@CrawledPropertyId int,
@SampleCount int
AS
set RowCount @SampleCount
SELECT
( DP.strVal + ISNULL(cast(DP.strVal2 AS nvarchar(2000)), '') ) as 'SampleURL'
FROM
dbo.MSSDocProps as DP
INNER JOIN
dbo.MSSCrawledPropSamples as CPS
on CPS.DocId = DP.DocId
WHERE
CPS.CrawledPropertyId = @CrawledPropertyId
AND DP.Pid = 7
ORDER BY DP.strVal
set RowCount 0
</pre><br />
Normally I wouldn't dream of altering a SharePoint SProc, but seeing as we are still in beta and I'm approaching a deadline, I decided to make a slight adjustment:<br />
<br />
<pre class="brush: sql">alter PROCEDURE dbo.proc_MSS_GetCrawledPropertySamplesByPropertyID
@CrawledPropertyId int,
@SampleCount int
AS
set RowCount @SampleCount
SELECT
( ISNULL(DP.strVal,'') + ISNULL(cast(DP.strVal2 AS nvarchar(2000)), '') ) as 'SampleURL'
FROM
dbo.MSSDocProps as DP
INNER JOIN
dbo.MSSCrawledPropSamples as CPS
on CPS.DocId = DP.DocId
WHERE
CPS.CrawledPropertyId = @CrawledPropertyId
AND DP.Pid = 7
ORDER BY DP.strVal
set RowCount 0 </pre><br />
Now, when clicking on my Crawled Property, I get a proper page:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2o3JQDnofrSMJcE9t2tc6IL4TylENo4yq3YuZCGTDo1ilHVObldOFQ3D1QYm1eGvLKm__bmn9mYb0Mv3ob1288u7M3ksMtG-Rr3TV42iGFNS2pU0qDpk4loKd9N1bcbgYuYyBIfWTF2Y/s1600/fixed.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="497" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2o3JQDnofrSMJcE9t2tc6IL4TylENo4yq3YuZCGTDo1ilHVObldOFQ3D1QYm1eGvLKm__bmn9mYb0Mv3ob1288u7M3ksMtG-Rr3TV42iGFNS2pU0qDpk4loKd9N1bcbgYuYyBIfWTF2Y/s640/fixed.png" width="640" /></a></div>Mikehttp://www.blogger.com/profile/09866285427297333913noreply@blogger.com0