Wednesday, November 21, 2007

Set custom properties of a custom field in List Definition

Problem:

I made a custom field, named "CrossSiteLookupFilterFieldControl". This field has some properties:

...

<PropertySchema>

<Fields>

<Field Name="SelectedSite" DisplayName="Site" Type="Text" Hidden="TRUE" />
<Field Name="SelectedList" DisplayName="List" Type="Text" Hidden="TRUE" />
<Field Name="SelectedColumn" DisplayName="Column" Type="Text" Hidden="TRUE" />
<Field Name="FilterValue" DisplayName="Filter value" Type="Text" Hidden="TRUE" />
<Field Name="FilterQuery" DisplayName="Filter query" Type="Text" Hidden="TRUE" />

</Fields>

</PropertySchema>
...

I want to use this field in a Custom List, so I add the following to schema.xml of my List:

<Field ID="{222EF3E9-3470-4619-89EC-7FFCDA18A3FC}" Name="Segment"  SourceID="{F9C3BB43-1EAA-4ac1-8878-EDBD7AFF3B4C}" StaticName="Segment"  Type="CrossSiteLookupFilterFieldControl" DisplayName="Segment" />

This works. But if I want to add a value for the FilterQuery property of the field, something like this doesn't work automatically:

<Field ID="{222EF3E9-3470-4619-89EC-7FFCDA18A3FC}" FilterQuery="Test" Name="Segment"  SourceID="{F9C3BB43-1EAA-4ac1-8878-EDBD7AFF3B4C}" StaticName="Segment"  Type="CrossSiteLookupFilterFieldControl" DisplayName="Segment" />

Solution

In the constructor of your custom field, you can read out the SchemaXml, and find the desired attributes. This way, you can set the values of your custom field with the values in the attributes:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(this.SchemaXml);
XmlAttributeCollection fieldAttributes = xmlDoc.FirstChild.Attributes;

if (fieldAttributes["FilterQuery"] != null)
                this.FilterQuery = fieldAttributes["FilterQuery"].Value;

 

PS: More about custom field definitions later..

Tuesday, November 13, 2007

Upload an InfoPath attachment to a document library

To copy a file attachment in an InfoPath form to a document library, I used this code. The "workfiche" object is an instance of a class generated by the XML Class Generator. For more information about this tool, go to http://www.canerten.com/xml-c-class-generator-for-c-using-xsd-for-deserialization/

byte[] attachFile = workfiche.Vlerick_TP_GeneralInformation.Brochure.AttachDraftText;

//get filename
int namebufferlen = attachFile[20] * 2;
byte[] filenameBuffer = new byte[namebufferlen];
for (int i = 0; i < filenameBuffer.Length; i++)
{
          filenameBuffer[i] = attachFile[24 + i];
}
char[] asciiChars = UnicodeEncoding.Unicode.GetChars(filenameBuffer);
string filename = new string(asciiChars);

filename = filename.Substring(0, filename.Length - 1);

//upload to Input Documents doc library

SPFolder inputDocumentsList = web.GetFolder("InputDocuments");

inputDocumentsList.Files.Add(filename, attachFile);

Sharepoint: Programmatically set an alert on a task list for a User

This code adds an alert for all users in a site, with a filter: The task has to be assigned to the user, the status has to be Completed and the Start date has to be less than "today". In other words: the active tasks :-)

foreach (SPUser user in newWeb.SiteUsers)
{
SPAlert alert = newWeb.Alerts.Add();

alert.Filter = "<Query><And><And><Eq><FieldRef Name=\"AssignedTo\"/><Value type=\"string\">" + user.LoginName + "</Value></Eq><Neq><FieldRef Name=\"Status\"/><Value type=\"string\">completed</Value></Neq></And><Leq><FieldRef Name=\"StartDate\"/><Value type=\"datetime\"><Today/></Value></Leq></And></Query>";

alert.User = user;
alert.AlertType = SPAlertType.List;
alert.List = newWeb.Lists["Tasks"];
alert.EventType = SPEventType.Modify;
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.Update();

}

Friday, November 2, 2007

Modify Members Web Part in code

To set a Members Webpart to "Show people in group" and fill in the group it has to show, use following code:

SPWeb web = GetWeb();

SPWebPartCollection webPartCollection = web.GetWebPartCollection(web.Navigation.Home.Url, Microsoft.SharePoint.WebPartPages.Storage.Shared);
                    webPartCollection.Web.AllowUnsafeUpdates = true;
                    foreach (WebPart webPart in webPartCollection) {
                        if (webPart is MembersWebPart) {
                            MembersWebPart membersWebPart = (MembersWebPart)webPart;
                            membersWebPart.DisplayType = MembersWebPartDisplayType.GroupMembership;
                            membersWebPart.MembershipGroupId = newWeb.Groups["groupName"].ID;
                            webPartCollection.SaveChanges(membersWebPart.StorageKey);
                        }
                    }
                    webPartCollection.Web.AllowUnsafeUpdates = false;