Wednesday, March 3, 2010

Sharepoiont Object Model Tips

1. If you want a list to show up in the left side of Sharepoint site, use OnQuickLunch property, set the property to true, then update ... a few times ...

    2. When adding the fields to the list, if you want to add them to the DefaultView do not add them directly to the DefaultView object, like in list.DefaultView.ViewFields.Add .... Rather iterate through the collection of views of the list, get the one that is default and add to that object's viewFields collection.

    3. Do NOT modify properties of spList through indexer of collection, there is a bug which makes the modifications not to be saved. Rather have an object take the value and modify the object.
    Ex: USE
        spList = web.Lists[guid];
        spList.OnQuickLaunch=true;
    INSTEAD of
        web.Lists[guid].OnQuickLaunch=true;


NOTE: There is a great article about the above tips in Daniel's blog:  : 

http://geekswithblogs.net/DanielC/archive/2008/02/05/splist-sharepoint-object-model-considerations.aspx

    4. You can not add a custom field (that does not have a base type) to a collection , for example Resizable Image, the only way you can add it is with
        FieldCollection.AddFieldAsXmlSchema
   
   5. The only way you can put an user other than the ones in web.AllUsers in a SPFieldUserValue is
        SPUser newUser = web.EnsureUser(@"domain\username");
        web.AllowUnsafeUpdates = true;
        SPFieldUserValue userValue = new SPFieldUserValue (web, newUser.Id, newUser.LoginId);

EnsureUser is a rather interesting method. You will see more about this on my blog or I'll put a link to a blog that explaines it ...
   
    6. There is no property for a column to provide context menu edit link. The column with edit link can be extended as a custom column or easier, the default Title column can have its display name changed to title you want to have on the column with link menu.
  
    7. SPFieldDateTime's property Date Only and default value property set to Today in UI web , can be programatically set like this:

                SPFieldDateTime field = (SPFieldDateTime)list.Fields[columnName];
                field.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
                field.DefaultValue = "[today]";

    8.When adding a workflow programatically to a list perform the next steps:

        //SPListHelper.GetList is a helper function that iterates through site lists for the wanted list.
        //name -  The name of the workflow association (cob in our case)
        const string TASKS = "Tasks";
        const string WORKFLOWHISTORY = "Workflow History";
        string COMPANYONBOARDING = "Company On Boarding";
        SPWorkflowTemplate baseTemplate = site.WorkflowTemplates[COMPANYONBOARDING];
        SPList tasks = SPListHelper.GetList(site, TASKS);
                if (tasks == null)
                {
                    site.Lists.Add(TASKS,TASKS, SPListTemplateType.Tasks);
                    tasks = site.Lists[TASKS];
                }
                SPList wfHistory = SPListHelper.GetList(site, WORKFLOWHISTORY);
                if (wfHistory == null)
                {
                    site.Lists.Add(WORKFLOWHISTORY, WORKFLOWHISTORY, SPListTemplateType.WorkflowHistory);
                    wfHistory = site.Lists[WORKFLOWHISTORY];
                }
                SPWorkflowAssociation newAssociation =
                    SPWorkflowAssociation.CreateListAssociation(baseTemplate, name, tasks,wfHistory);
                newAssociation.AutoStartCreate = true;
                list.AddWorkflowAssociation(newAssociation );
                list.Update();

                SPListHelper.AddColumnToDefaultView(list, name);
    9. Person Or Group column appears in Sharepoint Object Model as SPFieldUser. A Person Or Group with Multiple selection and Show Field = Account, is programatically set as it follows:

                list.Fields.Add(columnName, type, true);
                SPFieldUser fieldUser = (SPFieldUser)list.Fields[columnName];
                fieldUser.AllowMultipleValues = true;                       // allow multiple selection
                fieldUser.SelectionMode = SPFieldUserSelectionMode.PeopleAndGroups;       // choose people OR groups
                fieldUser.LookupField = "Name";

No comments:

Post a Comment