Archive

Archive for the ‘Sharepoint’ Category

Error When talking to SharePoint Web Services in code

January 4th, 2012 No comments

I’ve been trying to talk to SharePoint’s web services in C# for a couple of hours now. Something I do in JS all the time, but rarely in a clients C# app.

I’ve been getting the same error over and over again, but keep getting an error when trying to do anything.

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

I had dutifully copied the example code from the MSDN site and got a web reference to the Lists Web Service of course

and commented out the URL code in the example because that points at their site, not mine, which is pointed to by my web reference.

/*Set the Url property of the service for the path to a subsite.*/ listService.Url = http://MyServer/sites/MySiteCollection/_vti_bin/Lists.asmx;

WRONG!!!!!

If you are getting this error then it is because you commented out the URL.

When you add a web reference you are adding a WSDL file which is then compiled.  The WSDL is not specific to your site location,

if you open up the WSDL XML the section right at the bottom is pointing to elsewhere, the root of the WebApplciation not the RootWeb / Site you are interested in.

<wsdl:service name="Lists">     <wsdl:port name="ListsSoap" binding="tns:ListsSoap">       <soap:address location="http://my.binaryjam.com/_vti_bin/lists.asmx" />     </wsdl:port>     <wsdl:port name="ListsSoap12" binding="tns:ListsSoap12">       <soap12:address location="http://my.binaryjam.com/_vti_bin/lists.asmx" />     </wsdl:port>
</wsdl:service>

You need that line in the code that sets the listService.Url value, without it you are going to go mad trying to track down this bug, cos I didn’t find the answer online, I had to use fiddler.  Of course this article will now exist. :-)

 

Categories: development, Sharepoint Tags:

Creating TFS task Items from Powershell – From SPDisposeCheck Output

May 27th, 2011 No comments

Now one for the SharePoint people.  You have ran SPDisposeCheck on the dire piece of code your lackey wrote (or youself for that matter) and forgot to run the SPDisposeCheck.  Whoosh loads of entries to validate and change the code for. 

Being good people using TFS for source code and task management you want an entry for each item to check.

Using the built in XML handling facilities from PowerShell this is an easy task.

First run SPDisposeCheck with the output set to XML You will get something like this

<?xml version=”1.0″ encoding=”ibm850″?>
<ArrayOfProblem xmlns:xsi=”
http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
  <Problem>
    <ID>SPDisposeCheckID_110</ID>
    <Module>BinaryJam.CrappyCode.dll</Module>
    <Method>BinaryJam.CrappyCode.ApplicationPages.EditSiteProperties.GetPortalRootWeb</Method>
    <Assignment>local2 := new Microsoft.SharePoint.SPSite(local0).{Microsoft.SharePoint.SPSite}get_RootWeb()</Assignment>
    <Line>0</Line>
    <Notes>Constructor called for Microsoft.SharePoint.SPSite but not assigned. This type should be assigned and subsequently disposed</Notes>
  </Problem>
</ArrayOfProblem>

 

You can Easily read XML files in PowerShell with a simple statement

$xmldata = 1(Get-Content c:\test\out.xml)

Accessing the Elements is then just object notation

$xmldata.ArrayOfProblem.Problem

Will return an array of problem items that can be accessed in a foreach $problem and with $problem.ID

So the full code to do this is

Read more…

Categories: development, Sharepoint Tags:

jQuery autocomplete for SharePoint Search Box

December 6th, 2010 3 comments

Whether or not SharePoint actually needs an autocomplete for it’s search box could be debated, and the manner in which I’m about to implement it, well that could be debated also. 

The point of this post was to use jQuery techniques to query a list via its web services and do something flash.  I’m doing it SharePoint 2007 as that is what I have handy and working. 

I will be upfront and honest right now, the code for this is a Mashup from two sources, Jan Tielens and the jQueryUI demo itself, both needed tweaking to come up with something new, but just so you know it’s not been rocket science, hard work was done for me, cheers Jan. 

Read more…

WSS3 – ItemAdded – deadlocked on lock

February 12th, 2010 3 comments

I’ve been having a problem with an app I wrote.  It works great, 99% of the time, but sometimes the ItemAdded event appears not to fire.

Now this is using a list with over 20,000 entries in it and its growing all the time.  An associated list that references this list as a lookup has over 144,000 entries in it.

The event firing problem is becoming more frequent now as more users start using the system, there are at least 20 occurances in that 20,000 items, so it has to be fixed.

Managed to track down an error in the log

System.Data.SqlClient.SqlException: Transaction (Process ID 61) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

It’s stack trace was similar too this (I cut it down a bit and changed some names, my naming is not this bad)

at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
   at Microsoft.SharePoint.SPListItemCollection.get_Item(Guid uniqueId)
   at BinaryJam.Core.Code.TicketListReceiver.ItemReceiver.ItemAddedActual(Guid SiteId, Guid ListItemId, String UserDisplayName)
   at BinaryJam.Core.Code.TicketListReceiver.ItemReceiver.<>c__DisplayClass11.<ItemAdded>b__e()
   at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state)
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at BinaryJam.Core.Code.ItemListReceiver.ItemReceiver.ItemAdded(SPItemEventProperties properties)

So as you can see, it explodes on the GetItem.

This was my stupid mistake, a mistake I bet many of you are making every day.

SPListItem item = List.Items[listItemId];

This I believe is the primary cause of the problem as this statement loads the complete List before returning the specific item I needed and as this list grows, remember 20,000 items already, its going to get worse.  Now for most of you this won’t be a problem as you will be just getting the ListItem from the parameters, in my case I have to run as elevated as Im doing lots of stuff with Security Groups, that the entering user may not be in, hence my needing to re-get the List Item in the right security context.

So if your having a problem like this remember, Always use the

List.GetItemByID(id); //Or
List.GetItemByUniqueId(guid);

This may or may not solve my problem I believe it will, if it doesn’t it’s still going to improve the performance of my application.  If your wondering, this is the only time I use this and don’t know why I did, the rest of the App uses getby id or SPQueries.

A Good Article on Performance I did actually read, hence the app performs pretty well, except for this one bug, which is now hopefully fixed.

 

 

 

 

WSS, Workflow, Who Actually approved this ?

November 5th, 2009 No comments

So I like many have a workflow that takes requests for Sites with a bunch of details for costing etc.  Then when approved those Sites are created and permissions allocated.

What we did not have was a record of who actually approved the sites.  Whilst this info is in the workflow history after a while that information gets purged, leaving us with Sites that were approved but by whom we know not.

So this is a simple case of changing the workflow and recording all of the approvers in the SPWeb properties, except that the approvers as we can tell happen to be Sharepoint Groups or AD groups, neither of which contain a history of membership, if AD does none I know how to access and certainly not easy for the customer and in a large membership we still would not know who actually did the deed.

I dug and dug for this and eventually found the User Login that actually did the change and here is how.

In your workflow you need a “Workflow Action” of EventName “OnTaskChanged”, this object in the code behind, when Invoked contains AfterProperties,

string user = OnApprovalTaskChanged.AfterProperties.ExtendedProperties[new Guid("d31655d1-1d5b-4511-95a1-7a09e9b75bf2")].ToString();

The above statement gets the User Login frmo the ExtendedProperties Collection.  It Stores the User under the key of that Guid in a User type field that refers to the Editor.

A list of fields can be found here .

So that’s it, took a lot of debugging and poking about but next time your looking for the actual approving user, take a look here.

Categories: development, Sharepoint Tags:

Sharepoint and ResX and a little tool

October 14th, 2009 1 comment

I am doing lots of work in Sharepoint that requires resource files in english and german.

With a variety of developers before me different styles of editing the foreign language files emerged.

I think that much better tool support for managing resx’s is required,  and I recently found one that is a great help to us with a vareity of resx’s in various states of translation and existence.

http://resxtranslator.codeplex.com/

This resx tool allows you to pick a file and it displays the nuetral and foreign side by side along with comments and allows you to edit them all at once.  Corking piece of work, some polish would go amiss but Im hard pushed to say where as it does exactly what I need it to do and I can pass it to a non-techie developer.  It still down to me to edit the comments first to identify what needs translating and what definately needs leaving alone.

If you doing multi-language work go and check this tool out it might just help.

WSS and PowerShell 2

July 31st, 2009 No comments

If you were one of the two people who read my last post on WSS and PowerShell I updated it a bit and it’s worth checking out the changes.

I thought this time I would just point out some of the very simple statements in PowerShell that differ from your usual C# so that you can quickly make a transition from one to another.

One of the first things any programmer might need to do in a script is conditions so here is a PowerShell condition and how it differs to c#

if ($i -eq 10)
{
    write "It's ten you know"
}

The comparison operator for equals is not "=" or "==" but a scripting style operator of -eq. So this is the first thing you’re going to have to remember.  Other operators are

-gt greater than,  -lt less than,  -ge greater than or equal to,  -ne not equal

There are more I found a simple table of them here.

When dealing with boolean responses its important to know that they are represented by $true and $false so a condition to test this would look like

if ($kidding -eq $false)

{

    write "How strange"

}

Now being good little programmers you’re bound to want to structure your scripts, so functions!  PowerShell has functions, but remember this is not a compiled language but a scripting language, this means you have to declare your functions before you can call them, this is not VBScript where you could have your main at the top and functions below, your execution code starts at the bottom after you declared your functions. 

So declaring a function can be as simple as (there are other ways but I’m sticking with this one for now)

function GoGetMyList($name)
{
    $list=$lists["MyList"]
    return $list
}

The function has a parameter of $name and returns $list.  Now the observant of you will have noticed that I created my $list on the fly giving it a scope of function level, but I access $lists whose scope is outside of the function, ok bad coding perhaps, but it’s to prove a point.

Variable scopes.  There are I believe three scopes  Global, Script, Stack (function) level.

Global scope is used in the PowerShell environment mostly you can set something of Global level in a script and it will be available on completion of the script in the scripting "Shell/Command Prompt" environment, if you are chaining scripts together this could be useful.

Script scope.  This is like a global or module level variable to your script code, once declared it’s accessible inside and outside all your functions

Stack/Function scope.   Declare a variable inside a function it’s scope and life-span are inside that function.

The weird thing is how you declare and reference these variables.  So here is how to define 3 scopes within a function

function GetBaseObjects($SiteUrl)
{

    $script:site = New-Object Microsoft.SharePoint.SPSite $SiteUrl
    $script:web = $site.RootWeb
    $script:lists=$web.Lists
    $script:list=$lists["MyList"]
    $item=$list.GetItemById(1)
    $Global:DispName=$item.DisplayName
    return $item
}

Now this is a thoroughly useless function but it demonstrates the differing levels.

$script:site = New-Object Microsoft.SharePoint.SPSite $SiteUrl

The $scipt:site  declares the variable at a script level accessible from another function or the script "main body", there is no need to pre-declare these variables because at EVERY assignment you make it sets the scope, this is a valid set of statements

function dave()
{
    $script:tellme="Script"
    write $tellme
    $tellme="function"
    write $tellme
}
dave
write $tellme

This will display "Script", "function" and "Script".  This is because PowerShell with search each scope for a variable and if not there it looks up the inherited variable scope tree, for want of better words.  The first time referenced we have a script level but the second time we have a function level and the third time the function itself is out of scope along with its variables so we access the script level one again.

So remember the order of your functions when setting scope level variables and ALWAYS when setting a variable to define the scope.

That’s all for this post, I’ll try and do another one encompassing error handling and arguments in a complete script that changes SharePoint settings.

Technorati Tags: ,,,

Categories: development, Sharepoint Tags:

WSS3 Event Receivers

July 23rd, 2009 No comments

I’ve been doing some work with List Item Event Receivers and thought I’d share my experiences.

First there are a bunch of events that can be raised, I won’t list all but here are some of the good ones

ItemAdding /  ItemAdded
ItemUpdating / ItemUpdated

There are also specific ways of dealing with the data in each of these events, those that are “ing” events then the changes have not been applied yet so you get the change values from the AfterProperties passed in, those ending in “ed” you can grab the List Item directly as changes have already happened.

My experiences have been problematic so here is a few points that may help you when using these events.

Attachments

If you want to get hold of the attachments being added to a List, you can’t. (This may differ for doc libraries I have not inspected this).  Attachments are not on the list item as it does not exist yet and they are not in the Properties either.  Anything you want to do regarding attachments you have to do in “ed” events.

ID’s

When in the “Adding” event the one thing you have not got is the List.ID value.  If you were hoping to create entries in another list that has a lookup field of this list, your out of luck. There is no ID available to you, neither is there an @@IDENTITY equivalent.

Synchronous/Asynchronous Calls

The different types of events run in different contexts. In an Adding event this runs in the context of the Add from the page itself, after hitting Add in NewForm.aspx it will execute this Adding event code under the w3wp.exe process.  This is very handy when debugging as you can attach to the w3wp process and just debug.

**I have found that Added/Updated events are not called like this they are executed, I believe, I can’t be sure, by OWSTIMER, but regardless of the process, they are not executed immediately after the Adding events.  This can be witnessed by debugging the process and placing a breakpoint on the first line of Added code, After you hit “Add” the page refreshes and shows the list, however your breakpoint code is still waiting so this is on another thread of execution to your Adding code.

**Absolute nonsense.  The Added events also fire on the w3wp process but they do fire on a separate thread.  I have had problems in the past where the Added/Updated events do not stop on my breakpoints but it is not because they happen in the timer service, it’s some other reason.  I tried it just and it breaks on both adding and added.

This sounds like no big deal, in my example I needed to format the Title field to be the ID + “Mask”.  I did not have access to the ID until Added event.  After adding the list item the list view would re-appear with a blank title.  This was unacceptable to the client so I had to populate the field with some text “to be assigned”.  Later the Adding event kicks in has access to the ID and sets the Title to the Masked value I need.  The users on refresh of the list got what they wanted.

I know its a fudge, but you can’t do it in the event you cant use calculated fields or any of those tools this is what I had, but it’s worth knowing your “ed” events happen on a separate thread.

Enable/DisableFiring

In the “ing” events above, your receiver may need to alter the data, However you will be altering the data on the List.  If in the Adding event you set a field and update the list item then it will call the Updating/Updated events.  This is not normally what you want to happen as you might get into an endless update loop.

For this very purpose the SPItemEventReceiver Class you inherited can turn off this event firing. So in your event handler you can execute

this.DisableEventFiring();

This will stop all further event firing on this thread.

this.DisableEventFiring();
listItem.Update();
this.EnableEventFiring();

Really BIG HINT, always turn it back on again immediately.

Exception Handling

If you have code that MUST execute, like setting security, and other code that must also execute but if it broke you can live with it then organise the order of execution and consider multiple update() calls.  This is not a transaction, you have no rollback if your event fails, you just have a fail. This is also not exactly efficient either.

Consider the example where you set item level security based on a fields value, then emailing the customer or whoever.

Normally you might

SPSecurity.RunWithElevatedPrivileges(delegate() { ItemAddedElevated(properties.ListItem); });
private void ItemAddedElevated(SPListItem ListItem){
    this.DisableEventFiring();    SendEmails(ListItem);

    SetSecurity(ListItem);    ListItem["Sent"] = "Yes";    this.EnableEventFiring();

}

Obviously Bad Code. No exception handling, email servers go wrong, the SMTP format may not have been validated causing exceptions in the Sendmail bit, this will error, write to the log and never be heard of again, meanwhile you have bad security in your application. Also look at all that code being executed with events turned off, and what happens if an exception occurs before event firing is turned on, I don’t know, might not be good though.

Rewrite this code

SPSecurity.RunWithElevatedPrivileges(delegate() { ItemAddedElevated(properties.ListItem); });
private void ItemAddedElevated(SPListItem ListItem){
    SetSecurity(ListItem);
    ListItem["Sent"] = "Yes";
    this.DisableEventFiring();
    ListItem.Update();    

    this.EnableEventFiring();
    SendEmails(ListItem)

}

This slight change is less likely to go wrong but it’s still not brilliant is it, a bunch of try catch finally’s would be good here, but I’ll let you figure that out.

That’s all I can think of for now.

Categories: development, Sharepoint Tags:

WSS and PowerShell

July 23rd, 2009 6 comments

The more I do with SharePoint the more and more I an finding a use for PowerShell. I am using WSS as an application platform and using lists etc as a datasource.  Providing what you want is not huge amounts of referential integrity and transactional control etc it works pretty well.

But when things go wrong, often the only way to quickly get in a fix something in a structured, repeatable and documented manner is PowerShell.  Hell and its fun too.

So as a Sharepointy person what do you need to learn to get you quickly started.

First go and install PowerShell, this is version 1 which is what I am using, version 2 will be great but is not in production yet, so try getting that on your live servers, if they let you, sack them – pre-release code on production servers pah!.

Next go and download Quest’s PowerGUI.  This comes with the fantastic script editor, that allows stepping through code and debugging, i tend to use this for all my PowerShell development, I’m used to Visual Studio so being confronted with a command shell environment can be a little daunting.

Now if you go looking on the web at powershell there are some great resources and experts, I am not one of them I barely know PowerShell at all, I just rely on some simple things to get it working with SharePoint.

The biggest thing about PowerShell is you can just create .NET objects in it.  Yep just go ahead and create an SPSite object and away you go.

So lets dive into an example straight away

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$site = New-Object Microsoft.SharePoint.SPSite "http://localhost/products/MyWSSApp"
$web = $site.RootWeb
$lists=$web.Lists

$list=$lists["MyList"]

$item=$list.GetItemById(1)

$item["Title"]="Jeff"
$item.Update()
$item=$null $list=$null
$lists=$null
$web=$null
$site.Dispose()
$site=$null

 

This code will be executed on a machine with WSS installed on it, you can’t remote it from a box without PS and WSS installed. It’s LOCAL.

The first thing we do is load the assembly, without which PS knows nothing of the objects. 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

the next thing I’m doing is to create a reference to my site collection

$site = New-Object Microsoft.SharePoint.SPSite "http://localhost/products/MyWSSApp"

The New-Object statement is PowerShell’s way of creating a .NET object, the C# equivalent would be

SPSite site = new SPSite(http://localhost/products/MyWSSApp)

There is no need to declare the type in PowerShell, although it is possible to cast types, which you will need later on in your Scripting, but not today.

All variables in PowerShell are prefixed with a $ sign.

In the next sequence of statement you can see I easily manipulate the .NET Sharepoint object model from within PowerShell.  I can grab the RootWeb and assign it to a new PowerSHell variable and likewise with the list and a list Item.  Then a simple data change and list update.  All things you will be used to doing in C# can be done easily ni PowerShell to.

$web = $site.RootWeb

$lists=$web.Lists

$list=$lists["MyList"]

$item=$list.GetItemById(1)

$item["Title"]="Jeff" $item.Update()

Notice that I still deliberately DISPOSE of the site, this is still important to do even in powerShell.

Technorati Tags: ,,

Categories: development, Sharepoint Tags:

WSS 3 – Site Provisioning / List Security

July 13th, 2009 No comments

I’ve been having some more fun with Site Provisioning.  I have a requirement that in my Prov code I need to alter groups.  For most of my work I’ve been altering RoleAssignments in SPWebs with no trouble, however I came to do the same with a list as in an SPWeb.

Now I’m not sure if you can do security in the ONET but for my requirements I have $Resources that need concatenating together to form the group names (don’t ask).  Which it can’t do so it has to be done as a site provisioning thing.

What I noticed though is in my list, "of parent SPWEb of Parent SPWeb(RootWeb)" things where not as they were supposed to be.

The security on the list was completely wrong, as in it stated it had Unique permissions, when actually it does not, it will have when I’m finished with it, but at Site Provisioning time it should not.  Also all the groups were relating to the Site Collection level not the custom groups of the sub site it should have inherited.

It’s as if none of the list processing for fixing up the security on the list has happened, as it had done previously on SPWeb objects.

After Large amounts of digging I found that at time of Site Provisioning the List.Update() method has not been called. Calling this on the list before I do my processing, resolves all security issues.

So if having trouble provisioning list security remember the lists still in flux till you call the Update, whereas the SPWeb objects are not.  Weird, but true.

Technorati Tags: ,,
Categories: development, Sharepoint Tags: