SPList: Sharepoint Object Model Considerations (Update list properties)

I noticed a weird behavior of the Sharepoint Object Model when trying to set the attributes of an existing list of a Sharepoint site.

 The following code works as you expect:  

spList = web.Lists[“MyList”];
spList.OnQuickLaunch=true;

spList.Update();


but not this one:    


web.Lists[“MyList”].OnQuickLaunch=true;

web.Lists[“MyList”].Update();

 It is a “by design” behavior even if most of you considered it a bug. I had to use the reflactor in order to demonstrate my affirmation.

 The code statements that are important for us were highlighted.

 The following code is the indexer implementation of the SPListCollection class (including the other methods used by the indexer):

 public SPList this[string strListName]

{

    get

    {

        return this.GetListByName(strListName, true);

    }

}

 internal SPList GetListByName(string strListName, bool bThrowException)

{

    this.EnsureListsData(null);

    int num = 0;

    while (num < this.m_dwCount)

    {

        if (!SPUtility.StsCompareStrings(strListName, (string) this.m_arrListsData[(int) ((IntPtr) 4L), (int) ((IntPtr) num)]))

        {

            num++;

            continue;

        }

        return this.CreateSPList((uint) num);

    }

    if (bThrowException)

    {

        throw new ArgumentException();

    }

    return null;

}

 private SPList CreateSPList(uint index)

{

    SPBaseType type2 = (SPBaseType) Convert.ToUInt32(this.m_arrListsData[8, index]);

    if (type2 == SPBaseType.DocumentLibrary)

    {

        SPListTemplateType type = (SPListTemplateType) this.m_arrListsData[10, index];

        switch (type)

        {

            case SPListTemplateType.PictureLibrary:

                return new SPDocumentLibrary(this, this.m_arrListsData, index);

        }

        return new SPPictureLibrary(this, this.m_arrListsData, index);

    }

    if (type2 != SPBaseType.Issue)

    {

        return new SPList(this, this.m_arrListsData, index);

    }

    return new SPIssueList(this, this.m_arrListsData, index);

}

 SPList class:

 internal SPList(SPListCollection lists, object[,] arrListProps, uint iRow)

{

    this.m_Lists = lists;

    this.m_arrListProps = arrListProps;

    this.m_iRow = iRow;

    this.m_ListAttributesDict = new ListDictionary();

}

 As you can see, the indexer creates a SPList every time the indexer is called.

If you analyze the non-working code again it will be clear enough:

 A new SPList is created and its OnQuickLaunch property is set to true:

 web.Lists[“MyList”].OnQuickLaunch=true;

 A new SPList is created and the Update method is called, but nothing special happens because the OnQuickLaunch property was set on other SPList instance:  

 web.Lists[“MyList”].Update();

 The same behavior was observed also when working with the DefaultView (please see my previous post about that). I’m convinced that this behavior is applicable as well to other sharepoint objects.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Spam Protection by WP-SpamFree