Friday, December 16, 2011

The query cannot be run for the following DataObject: GetCommonManager

I got that message when I previewed an InfoPath form (SharePoint Server 2010, InfoPath 2010):

The query cannot be run for the following DataObject: GetCommonManager 
InfoPath cannot run the specified query. 
The SOAP response indicates that an error occurred on the server: 
Server was unable to process request. ---> Object reference not set to an instance of an object.

I knew that new User Profile Service Application had been created and it used to work with old one without issues. The UserProfileService web service was healthy and calls for GetUserProfileByName were proceeded well. It just stuck with GetCommonManager call.

After some investigation I found My Site host was not defined in that brand new User Profile Service Application. When I defined it My Site Settings for the application  GetCommonManager started working again.

Hope that helps save time someone.


Wednesday, April 20, 2011

Title column value in ItemUpdating event for Document Library

I had to add some logic in ItemUpdating event receiver when anything was changed except one particular column. This had to work for both lists and libraries.
There some good posts about differences in properties for lists and libraries, e.g. this one from Randy Williams.

My code was

string[] ignoreFields = new string[] { "myField" };
foreach (SPField field in properties.ListItem.Fields)
{
if (!ignoreFields.Contains(field.InternalName) &&
properties.AfterProperties[field.InternalName] != null &&
(properties.ListItem[field.Id] == null ||
properties.ListItem[field.Id].ToString() != properties.AfterProperties[field.InternalName].ToString()))
{
// do something
}
}


This worked well for lists but I struggled with Title column in libraries.
I found properties.AfterProperties["Title"] is always null for libraries even when Title was changed.

After intensive googling I found that for libraries properties.ListItem.Title is mapped to properties.AfterProperties["vti_title"].

So, I had to add this

if (properties.List is SPDocumentLibrary && properties.ListItem.Title != properties.AfterProperties["vti_title"])
{
// do something
}

Thursday, March 10, 2011

No paging on SharePoint groups page

I don't know exact reason but this was a fact: I have more than 30 SharePoint groups in particular site collection and no paging at all. For other site collections paging is there.

I was very annoying: I had to filter by group name to have access to members list for groups with name started with 'k' and below.

I googled and found this is actually a hidden view of User Information List. Looked like it had 'Limit the total number of items returned to the specified amount' set under 'Items limit' instead of 'Display items in batches of the specified size'. So, the question was How to get access to its View Edit page?

Actual View Edit page is an application page in layouts folder (_layouts/viewedit.aspx) and it accepts two parameters in query string: List and View ids.

What I did:

- went to the groups page (http://spSite/_layouts/groups.aspx)

- opened page source and found list and view guids there. The answer was searching by WebQuerySourceHref= phrase. I found url with this values there.

- went to http://spSite/_layouts/viewedit.aspx?List={630F7EE7-1EA5-45F5-A162-4523647E1564}&View={132D30FA-EAD7-4134-B328-DB873CE82BD0}
(ids are mine :)

- changed the settings under 'Items limit'

Thursday, March 3, 2011

Accessing mixed authentication web app with Client Object Model and Web Serices

I have mixed authentication web app in SharePoint 2010: both Windows (NTLM) and Forms based.
When I tried to access it using Client Object Model with my NTLM credentials I got 401 (Unauthorized) exception. I googled and found this solution

...
clientContext.ExecutingWebRequest += new EventHandler(clientContext_ExecutingWebRequest);
...
}

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}


This works well for me.

Then when I tried to access same site using Lists.asmx web service with same NTLM credentials I got exactly same exception.
I added this method to the Web Service wrapper class (it's in Web References > ListWebService > Reference.map > Reference.cs in my case)


protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.WebRequest wr = base.GetWebRequest(uri);
wr.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
return wr;
}

That solved the issue as well.

Tuesday, March 1, 2011

How to attach files to list item using Client Object Model

No way. It is just impossible to do that using Client Object Model.

This is my story.

The task was to copy list item with attachments to another location of separate farm. Everything was going fine. There are tons links in Google how to work with SharePoint 2010 Client Object Model. So my code was like that:


foreach (string attachment in sourceItem.Attachments)
{
string fileUrl = SPUtility.ConcatUrls(sourceItem.Attachments.UrlPrefix, attachment);
SPFile file = sourceItem.Web.GetFile(fileUrl);
Stream fs = file.OpenBinaryStream(SPOpenBinaryOptions.SkipVirusScan);

string attachmentPath = string.Format("{0}/{1}/Attachments/{2}/{3}", SPUtility.ConcatUrls(targetContext.Web.ServerRelativeUrl,"Lists"), targetListName,
targetItem.Id, attachment);

Microsoft.SharePoint.Client.File.SaveBinaryDirect(targetContext, attachmentPath, fs, true);
}


Looks good, doesn't it? However when I ran it ... ups ... I got 409(Conflict) error message. After debugging and checking attachmentPath was correct (e.g. /Lists/testList/Attachments/10/testFile.doc) I googled the error and found the most possible reason was the folder not found on the target. Actually it wasn't. Folder for item id (10) in attachmentPath is never exist in this stage by design because targetItem had been just created. But SaveBinaryDirect doesn't create folder. It just save file and expects correct path provided.
I tried to create the folder with ListItemCreationInformation object but without any success. That technique is for creating ordinary folders and doesn't work for creating folders under Attachments folder. Then I found this stack overflow thread. Looks like Microsoft had confirmed it is not possible to achieve that using Client Object Model.

The only one way for now is using Lists.asmx web service.
So, I added web reference to the web service. In VS 2010 I did right click on References, then 'Add Server Reference', then clicked 'Advanced...' button on the bottom and provider web service url (any existing url e.g. http://spdev/_vti_bin/lists.asmx in my case).

This is my final code

ListsWebService.Lists objLists = new ListsWebService.Lists();
objLists.Credentials = targetContext.Credentials;
objLists.Url = SPUtility.ConcatUrls(targetWebUrl, "/_vti_bin/lists.asmx");

foreach (string attachment in sourceItem.Attachments)
{
string fileUrl = SPUtility.ConcatUrls(sourceItem.Attachments.UrlPrefix, attachment);
SPFile file = sourceItem.Web.GetFile(fileUrl);
byte[] fileContent = file.OpenBinary(SPOpenBinaryOptions.SkipVirusScan);
objLists.AddAttachment(targetListName, targetItem.Id.ToString(), file.Name, fileContent);
}


Hope it helps someone make things clear.

Friday, February 18, 2011

Setting time out for Silverlight web part

There is the way to set time out for Silverlight web part. This time out happens when web part can't load xap file.
The value is hard-coded within web part and equal 5 sec which is not so much.
First of all where this value is hard-coded? If you viewed source of the page with Silverlight web part you could found javascript line like that

window.setTimeout("_spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999();", 5000);

This is our bad guy. Value 5000 is hard-coded and function name contains web part Id (ctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999 in my case).

So, the easiest way to handle it is to eliminate time out at all by creating same named dummy JavaScript function:

1. Go to page source and search for _spCheckIfSilverlightPluginIsLoaded_WebPart phrase. You suppose to find function which starts with this phrase. Copy function name to clipboard.

2. Add content editor web part after Silverlight web part and put empty JavaScript function with saved name there like that

<script type="text/javascript">
function _spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999(){}
</script>


where _spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999 is a function name I've found on my page.

> Update! - one more (even better!) workaround:

You can override _spSetSLPluginNotLoadedErrorMessage function which is called by web part if XAP file wasn't loaded in 5 sec.

1. Just drop this tag in Content Editor web part on the page:


<script type="text/javascript"> function _spSetSLPluginNotLoadedErrorMessage(wpId){ 
} 
</script>

> end of update

This is working just fine if you have couple pages with Silverlight web part and you are Ok do not have any time out.

Better but little bit more complicate way is creating javascript to set a new time out value. Main idea is put some code in our dummy function with new time out value. I created a web part for that. It is on codeplex.
It scans the page for Silverlight web parts and renders javascript for each instance. It has just one property - time out :)

Please note this web part should be on your page after Silverlight web part.

How I tested it:
I put two Silverlight web parts to my page with wrong reference to xap file. I got time out in 5 secs on both. Then I put my web part in between. I got time out in 15 secs (my default value) for the first Silverlight web part and still 5 secs for the second.

You can check out wsp package and source code.

Have fun with Silverlight!

Friday, January 7, 2011

WSS-like breadcrumb in SharePoint 2010

This was the first user's request after migration had been done. It wasn't very important. Just annoying. I played some time with site navigation and ended up with jQuery. There is a kind of WSS-like breadcrumb on SharePoint 2010 master page but now it is a pop-up. The idea is to put it to page in usual way.

1. Add reference to jQuery into your master page, e.g.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


just before tag </head>

2. Add breadcrumb holder div and jQuery next to it where you want to see it

<div class="wss-BreadCrumb"></div>
<script type="text/javascript">
$("a.s4-breadcrumbRootNode").clone().appendTo('div.wss-BreadCrumb');
$("a.s4-breadcrumbNode").clone().appendTo('div.wss-BreadCrumb');
$("a.s4-breadcrumbCurrentNode").clone().appendTo('div.wss-BreadCrumb');
$("<span>></span>").insertAfter('div.wss-BreadCrumb a.s4-breadcrumbRootNode');
$("<span>></span>").insertAfter('div.wss-BreadCrumb a.s4-breadcrumbNode');
$("span.s4-breadcrumbCurrentNode").clone().appendTo('div.wss-BreadCrumb');
</script>


3. Add styles to make it looks like WSS breadcrumb

div.wss-BreadCrumb
{
margin-bottom:5px;
display: inline-block;
}

div.wss-BreadCrumb span
{
margin-left:5px;
margin-right: 5px;
}

div.wss-BreadCrumb span.s4-breadcrumbCurrentNode
{
margin-left:0px;
margin-right:0px;
}


div.wss-BreadCrumb a.s4-breadcrumbNode, div.wss-BreadCrumb a.s4-breadcrumbCurrentNode,
div.wss-BreadCrumb a.s4-breadcrumbRootNode
{
color: blue;
}

Thursday, October 21, 2010

DisableEventFiring is obsolete in SharePoint 2010

You can check this in msdn. However DisableEventFiring()/EnableEventFiring() still works as per Reflector.
Recommended way now is to use new property EventFiringEnabled. This gives us an option to check current status and save it. I found a great post from Adrian Henke and modified his code using this new feature.



The class DisabledItemEventsScope disable/enable event firing for the current thread and could be used in that manner at any place including code behind for the page or web part



BTW: There are couple useful SPList extensions to manage SPListItemEventReceiver collection

Monday, October 11, 2010

SPUrl for WSS 3.0 or SharePoint Foundation

No, expression like <% $SPUrl:~sitecollection/Style Library/mystyles.css %> doesn't work for WSS or SharePoint Foundation. This is part of Publishing Infrastructure and you have to buy MOSS or SharePoint Server license to get it.

So, let's create our own ExpressionBuilder. I didn't implement 'language' support - obviously I need just Site and SiteCollection.


...
using System.Web.Compilation;
using Microsoft.SharePoint;

...
public class WSSUrlExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression
(System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
CodeTypeReferenceExpression thisType = new CodeTypeReferenceExpression(base.GetType());

CodePrimitiveExpression expression = new CodePrimitiveExpression(entry.Expression.Trim().ToString());

string evaluationMethod = "GetKeyValue";

return new CodeMethodInvokeExpression(thisType, evaluationMethod, new CodeExpression[] { expression });
}

public static object GetKeyValue(string expression)
{
SPWeb web = SPContext.Current.Web;

string key = "~SiteCollection";
if (expression.IndexOf(key, StringComparison.InvariantCultureIgnoreCase) == 0)
return web.Site.Url+expression.Substring(key.Length);

key = "~Site";
if (expression.IndexOf(key, StringComparison.InvariantCultureIgnoreCase) == 0)
return web.Url+ expression.Substring(key.Length);

return expression;
}

}
...


You need to put this class to some assembly and register it with your prefix(e.g. WSSUrl) in Web.Config


<configuration>
<system.web>
<compilation>
<expressionBuilders>
<add expressionPrefix="WSSUrl"
type="{your Namespace}.WSSUrlExpressionBuilder,{Assembly Name}"/>
</expressionBuilders>
</compilation>
</system.web>
</configuration>


Assembly Name should be fully qualified if you put the assembly into GAC.

Thursday, October 7, 2010

SharePoint: How to get document icon image

There is a method in SPUtility which makes it much easier. It takes image file name from docicon.xml, and should work Ok even if the file name is not follow the pattern ic{doc ext}.gif


string docIcon = SPUtility.ConcatUrls("/_layouts/images/",
SPUtility.MapToIcon(item.Web,
SPUtility.ConcatUrls(item.Web.Url,item.Url), "", IconSize.Size16));


There is a good post about SPUtility.MapToIcon vs SPFile.GetIcon

Friday, September 10, 2010

SharePoint 2010: How to add back QuickLaunch to the web part page.

Actually this is not adding but just removing hiding functionality:

1. Remove this code/css from the page

<SharePoint:UIVersionedContent ID="WebPartPageHideQLStyles" UIVersion="4" runat="server">
<ContentTemplate>
<style type="text/css">
body #s4-leftpanel {
display:none;
}
.s4-ca {
margin-left:0px;
}
</style>
</ContentTemplate>
</SharePoint:UIVersionedContent>


2. Remove this place holder from the page

<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>

Thursday, September 2, 2010

Restart SPTimerV4 service remotely in the SharePoint farm

There is a PowerShell script which restarts SPTimerV4 service remotely on every application server within a farm. This is useful when deployment of a SPTimerJob has been done.

It should be run on one of the application servers under SharePoint 2010 Management Shell, and you have to have enough permissions of course.

foreach ($server in (get-spserver | Where {$_.Role -eq "Application"}) )
{
write-host $server.Name
$service = Get-Service -computer $server.Name -name SPTimerV4
if ($service.Status -ne "Stopped")
{
$service.Stop()
Do
{
start-sleep -s 5 | out-null
write-host '.' -noNewLine
$service = Get-Service -computer $server.Name -name SPTimerV4
}
While ($service.Status -ne "Stopped")
write-host $service.Status
}
else
{
write-host "Stopped already"
}
if ($service.Status -ne "Running")
{
$service.Start()
Do
{
start-sleep -s 5 | out-null
write-host '.' -noNewLine
$service = Get-Service -computer $server.Name -name SPTimerV4
}
While ($service.Status -ne "Running")
write-host $service.Status
}
}

It shows the state of the service for each server. So, you can go and restart it manually if something was going wrong.

Tuesday, August 31, 2010

Hide SharePoint 2010 Ribbon Control from public users

There are some posts about how to hide SharePoint 2010 Ribbon from users with least permissions. One example is here. But it trims the ribbon from the page, which could cause some issues. Most known is a scroll bar disappearing.
BTW: there is a perfect article about SharePoint 2010 Ribbon Control from its designer.


There is the way how to real hide it depending of current user permissions (owner permission in this example). Just add this to the head section on master page.

<style type="text/css">
div#s4-ribbonrow {
display:none;
}
</style>

<Sharepoint:SPSecurityTrimmedControl runat="server"
Permissions="AddAndCustomizePages">
<style type="text/css">
div#s4-ribbonrow {
display:block;
}
</style>
</Sharepoint:SPSecurityTrimmedControl>