HiddenNetwork.com Banner
Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Monday, June 02, 2008

Things to think about when planning a SharePoint solution

Here is a list of things that people seem to forget when planning a sharepoint solution:

  1. There will be many users using the solution at the same time. Make sure your solution is able to deal with that.
  2. When developing a web part or a custom field control, always remember that more than one instance of it may be on the same page. Lack of testing of what happens if you put two web parts side by side on the same page may cause scripts to clash if you didn't plan right.
  3. Most (all) sharepoint implementations have more than one front end server. Plan for your solution to be able to spread across multiple servers, and make sure that when it does, it load-balances!.
  4. Dont forget logging of errors in this case should make it easy for the admin (or yourself) to find where the error was- regardless of the server that was serving the client when the error occured.
  5. Performance! users do not like to wait more than 3 seconds for a page to load - and if your web parts are not performing well, you will ruin the browsing expirience in the site. Use caching techniques to improve page loads.
  6. Remember when caching that some things should be cached on a user level, and some can be on the application level.
  7. In web controls - remember error trapping. This may be obvious to anyone who ever developed anything, but this happens so often that I have to just say - I don't care what you do with your errors - but don't crash my page! Make sure everything is nicely wrapped up in try-catch, and never throw an error to a page. Remember that other people (controls) are using it as well - it is not yours.
  8. Develop for deployment. Make sure you plan a WSP package, and document well the list of all the things that will be required that cannot be deployed using a WSP package (changes to the web.config aside from safecontrols for example)
  9. CAS!

Tuesday, March 13, 2007

Move file in event handler causes "No item exists" Error

I have been playing around with an event handler that moves files after they have been uploaded to a document library. This is useful in scenarios where you want people to upload files (or post infopath forms) to a library, then analyze the meta data and move it to another library automatically based on what the user chose in the meta data.
A cool example of how we may use this is an infopath form where the user's details are getting pulled from a database, including his "state" attribute. The event handler, when seeing the user is from a specific state, moves the document to a document library where it will be accessabile only by the people in the same state. This way alerts can be set be state and a seperate workflow can be developed for each state and so on.

The code is actually easy. I don't use the SPFile.MoveTo() function since it doesn't support moving to other sites, and that may be required. So I wrote a function that gets the source file (SPFile) and the target folder (SPFolder), copies the file by reading it's stream and then deletes the source file. This ofcourse can be improved in the future.

private static void MoveFile(SPFile fileToMove, SPFolder targetFolder)
{
    
        
Stream s = fileToMove.OpenBinaryStream();
        targetFolder.Files.Add(fileToMove.Name, s);
        targetFolder.Update();
        fileToMove.Delete();
       
}

The problem I discovered that even though I was running the code in the ItemAdded event, the code would run when the file is uploaded. This causes a big problem, since sharepoint 2007, after uploading the file checks if there are any properties (meta data) it needs to ask the user for, and redirects him to the meta data entry screen. But my event handler already deleted the file!
So every time the user uploads a file, he will see: "No item exists at http://site/web/library/Forms/EditForm.aspx?Mode=Upload&CheckInComment=&ID=23&RootFolder=/web/library&Source=http://site/library/Forms/view.aspx. It may have been deleted or renamed by another user."

I have tried using the "ItemCheckedIn" event instead, with no luck - it is not called when a file is uploaded. It looks like the sequence of events is:

  • ItemAdding
  • ItemAdded
  • ItemUpdating (this is after the user enters the metadata)
  • ItemUpdated
So from this we can conclude (and I tested to confirm) that if you want to move a file on upload, do it in the ItemUpdated event, or the ItemUpdating event.

The problem is, if the user presses "Cancel" in the metadata window, the file will remain in the library, since the "Updating" events didn't trigger.
What to do? I honestly don't know. It seems we cannot develop an event handler that moves files automatically and will trap all documents uploaded, without causing errors on the screen for the user.

Sunday, December 17, 2006

CAML Queries return all items despite of filter

This had me puzzled for a while, so let me spare you the agony.

I was writing code that queried a list using the GetListItems function of the SPList object. The query I was using was from the U2U Caml Creator (great application, even with the bugs) and was working on that application perfectly - filtering my list to return only the items I wanted.
But in my web part the query would return all items in the list, ignoring the filter I set.

After some tests I found the problem. Apperantly the SPQuery object automatically wraps your query with the "<query>" tag, and since the U2U Caml Creator application also generates queries with the "<query>" tag, you actually have two "<query>" tags in your query, which makes sharepoint ignore the filters you set in the query.

So next time you are using the SPQuery object, remember the Query property should receive a CAML query, but without the "<query>" tag. Only whats under it.