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?