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;
}