Monday, December 15, 2008

Changing fields by code - common oversight

I just helped someone on the OzMOss mailing list who had the following problem - he was trying to change a SPField object, but it was not getting updated, despite of no errors being thrown. Here is a sample of the code he was using:

Guid listID = web.Lists.Add("test", "", SPListTemplateType.PictureLibrary);
SPList list =web.Lists[listID];
SPField myField = list.Fields.GetFieldByInternalName("Title");
myField.Title = "TEST";
list.Update();
The problem with that code is a common oversight that developers who are new to the SharePoint object model usually make - the field object has to be updated. updating the list will have no effect, since the object that was modified was a column on the list, not a property of the list. The right code would be:
Guid listID = web.Lists.Add("test", "", SPListTemplateType.PictureLibrary);
SPList list =web.Lists[listID];
SPField myField = list.Fields.GetFieldByInternalName("Title");
myField.Title = "TEST";
myField.Update();

Tuesday, November 25, 2008

How to copy attachments from one list item to another

The microsoft support article for WSS 2 shows us how to download attachments from a list item. Basically, the attachments for a list item are stored as SPFile objects under a hidden folder in the list where those attachments are (a folder called "Attachments") - where each list item that has an attachment has its own folder - with the ID of the item being the folder's name. Here is a code sample for a function to copy attachments from one item to another.

private void CopyAttachments(SPListItem sourceItem, SPListItem targetItem)
{
  try
  {
     //get the folder with the attachments for the source item
     SPFolder sourceItemAttachmentsFolder =
         sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
      //Loop over the attachments, and add them to the target item
     foreach (SPFile file in sourceItemAttachmentsFolder.Files)
     {
        byte[] binFile = file.OpenBinary();
        targetItem.Attachments.AddNow(file.Name, binFile);
     }
  }
  catch { }
  finally
  {
     sourceItem.Web.Dispose();
  }
}

Thursday, November 20, 2008

Custom fields with custom properties - important link

If you have developed a custom field type that has custom properties, and now want to create a feature that uses that custom field (to add a site column for example) and want to set the custom property, you may be puzzled how to set the custom property, since the schema of the elements file does not allow it.

I found that Péter Holpár has a solution - I tested, and it works. Basically, just add the custom property with a custom name space - for example, using my datevalidator field type from my teched presentation, you can define it as a site column thus (important bits are highlighted):

<?xml version="1.0" encoding="utf-8"?>
<!--
-->
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"
xmlns:custom="http://www.sharepoint-tips.com/customfields">
 <Field ID="{BBC8063E-252E-4fc0-9DC6-CA162512BB33}"
        SourceID="http://schemas.microsoft.com/sharepoint/v3"
        Type="DateValidationFieldType"
        Name="MyTestValidationField"
        StaticName="MyTestValidationField"
        DisplayName="My Test Validation Field"
        Group="Custom"
        Hidden="FALSE"
        ReadOnly="FALSE"
        Required="FALSE"
        RowOrdinal="0"
        custom:StartDateFieldName="Start Date"
        custom:EndDateFieldName ="Due Date"
 >   
 </Field>
</Elements>


I hope it solves your questions as well.

Thursday, November 13, 2008

Using the People picker - how to specify what can users choose

Just got this question in the Ask-The-Experts stand in Tech.ED europe. If I use the people picker like I showed in a previous article, how can I specify if the users can pick a user or a security group or a distribution list?

Well, the answer was in the SDK - there is a property "SelectionSet" that accepts a comma-delimited list of strings that say what can be picked.

For example, "User,DL,SecGroup" will allow users to pick user accounts, distribution lists and security groups. Choosing just "User" will allow only people accounts. The sharepoint people picker ususaly sets "User,SecGroup,SPGroup" or "User,SecGroup" or just "User"

Wednesday, November 12, 2008

My code for the custom fields presentation

As promised, here is the code I used. For the PowerPoint presentation I need to get approval, so wait for it.

The code has three samples of custom field types - one is a prime number validation field type (allows people to type only prime numbers), one is a date validation field that checks two other fields that the end date is after the start date (this is a good example for creating custom properties for the custom field) and the last is a sample how to create a custom look and feel for a custom field, without compiled code - just XML definition (CAML) in the renderpattern section. have fun!

Tuesday, November 11, 2008

Tech.Ed EMEA presentation

I just came out of my presentation in Tech.ED EMEA, and it was great fun! I showed how to develop custom field types, and was very well recceived. My current rating is 8.07 (which is pretty high, especially for a 400 level presentation) and here is some of the great feedback I got:
"I wish all teched sessions were this informative! Maybe some more time for questions."
"a fantastic engaging performance"
"Great session by Ishai Sagi. Keeping it to the topic and quickly answering questions in a good fashion. Great work. Kept me awake although I was rather sleepy (Jetlag? :-))"
"very good presentation, deep look inside sharepoint!!"
I am doing the same presentation again on thursday, and then I will be able to post the content here on the blog. anyone interested?

Tuesday, October 28, 2008

Throwing myself behind the site definitions

Recently on the sharepoint blogs people have begun discussing "site defintions - good or evil" again. I wanted to make my stand, but my fellow MVP Eric Shupps beat me to it and posted exactly what I think of site defitions in his blog.
So thanks Eric for pointing out what I think is right, and doing it so well. The bottom line? Site definitions are not evil, and when time will come to upgrading to sharepoint 14 (or whatever the name\number will be) then site definitions upgrade will be no worse than the features upgrade. Do your site definitions right and that will save a lot of headache, but using all kind of wierd workarounds just to avoid using site defintions is silly, and will not save you time upgrading.
That said, doing the site definitions the wrong way (get into very complex CAML work and use the site defintions as if you are still using sharepoint 2003) can be very detramental to both the current setup that you have, and to the future upgrade path. So make sure you know what you are doing, and follow Eric's advice. I agree with it 100%.

Sunday, October 26, 2008

Cool new free feature - download all files zipped!

This is a cool one - Mohamed Zaki (fellow MVP) just released an open source feature that allows users to download an entire document library as a zip file. Take a look at his blog post about it.

Friday, October 24, 2008

Application pool crash on win2008

This just happened to me - installed sharepoint on win2008, and for the first time ever, when I configured the farm search service, I did not choose "Use all web front end computers for crawling", but instead I selected my server. The result? Sharepoint mangled my hosts file, using IPv6 entries.
This in turn caused my application pools to crash with the error "A listener channel for protocol 'http' in worker process '6120' serving application pool 'SharePoint Central Administration v3' reported a listener channel failure. The data field contains the error number.".
Resolution? I fixed my hosts file, then disabled the IPv6 on my loopback adapter, and restarted the application pool - then I was able to go back to the search service configuration and undo what I did. That again changed my hosts file, so I had to edit it again and done! sharepoint is working again - ready for TechEd EMEA.
Talking about Tech.ED EMEA, I still didnt get any takers on the competition I started. Apparently a lot of people signed up to my presentation - enough for the teched administrators to ask me if I don't mind presenting twice (I don't - see me on tuesday or thursday). If you want to win a special prize from Australia, and are coming to Tech.ED Barcelona, comment here - and I will draw a prize from all the commenters who are there. Please leave full name.

Monday, October 20, 2008

New SharePoint book coming up - by me!

If you have a lot of end users asking you how to do stuff in sharepoint, maybe you should preorder this book. I have been writing it in the last few months, and am getting close to finishing it, and it is already up for pre ordering in Amazon.

This book is intended for non technical users.(Not for developers or administrators) who want a SharePoint companion to help them with day to day sharepoint tasks - things like "What is sharepoint?" and "How do I switch views in a list?" or "How do I create a view?" and so on.

I am very excited about this - and I hope it will be a success so I can write some more books - maybe the next one will be for you - the developer? But that will only be possible if this book is a success, since writing is a lot of effort. So, please, tell your friends, and get your purchasing officer to pre order some - it is on discount!