none
Why does creating my list items via Feature Receiver not create them in the correct order? RRS feed

  • Question

  • I'm creating some dummy items for my Promoted Links List using a Feature Receiver. The items are getting created, however, they are usually in a randomized order. Sometimes they follow the correct order in which they were sequenced in code, other times not. Is there a way for me to ensure that they are created in the same order as I coded them? Below is the helper and code call I use:

    Helper Method:

    public static void AddPromotedLinkItem(SPList list, string contentType, string title, string description, string bgImage, string linkLoc)
            {
                SPListItem newItem = list.Items.Add();
                newItem["ContentTypeId"] = list.ContentTypes[contentType].Id;
                newItem["Title"] = title;
                newItem["Description"] = description;
                newItem["Background Image Location"] = bgImage;
                newItem["Link Location"] = linkLoc;
                newItem.Update();
                list.Update();
            }

    Code call in Feature Receiver:

    private void CreateDummyLinks(SPWeb web)
            {
                SPList list = SPListHelper.GetSPList(web, "My Links");
                SPListItemCollection items = list.Items;
                string contentType = "My Links";
                
                SPListHelper.AddPromotedLinkItem(list, contentType, "Announcements", "", web.Url + "/PublishingImages/announcements.png", web.Url + "/announcements");
                SPListHelper.AddPromotedLinkItem(list, contentType, "News", "", web.Url + "/PublishingImages/news.png", web.Url + "/news");
                SPListHelper.AddPromotedLinkItem(list, contentType, "Files", "", web.Url + "/PublishingImages/files.png", web.Url + "/files");
    
            }
    EDIT: please take note that in my actual code call, I am creating around 10+ items rather than just 3 as above.



    • Edited by OCS.New Thursday, April 20, 2017 1:56 PM
    Thursday, April 20, 2017 1:55 PM

Answers

  • Hi,

    Please set the "Order" field(internal field name is "TileOrder") in your code to achieve it.

    public static void AddPromotedLinkItem(SPList list, string contentType, string title, string description, string bgImage, string linkLoc,int order)
    {
    	SPListItem newItem = list.Items.Add();
    	newItem["ContentTypeId"] = list.ContentTypes[contentType].Id;
    	newItem["Title"] = title;
    	newItem["Description"] = description;
    	newItem["Background Image Location"] = bgImage;
    	newItem["Link Location"] = linkLoc;
    	newItem["TileOrder"] = order;
    	newItem.Update();
    	list.Update();
    }
    
    private void CreateDummyLinks(SPWeb web)
    {
    	SPList list = SPListHelper.GetSPList(web, "My Links");
    	SPListItemCollection items = list.Items;
    	string contentType = "My Links";
    	
    	SPListHelper.AddPromotedLinkItem(list, contentType, "Announcements", "", web.Url + "/PublishingImages/announcements.png", web.Url + "/announcements",1);
    	SPListHelper.AddPromotedLinkItem(list, contentType, "News", "", web.Url + "/PublishingImages/news.png", web.Url + "/news",2);
    	SPListHelper.AddPromotedLinkItem(list, contentType, "Files", "", web.Url + "/PublishingImages/files.png", web.Url + "/files",3);
    
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by OCS.New Friday, April 21, 2017 12:55 PM
    Friday, April 21, 2017 8:24 AM
    Moderator

All replies

  • Hi,

    Please set the "Order" field(internal field name is "TileOrder") in your code to achieve it.

    public static void AddPromotedLinkItem(SPList list, string contentType, string title, string description, string bgImage, string linkLoc,int order)
    {
    	SPListItem newItem = list.Items.Add();
    	newItem["ContentTypeId"] = list.ContentTypes[contentType].Id;
    	newItem["Title"] = title;
    	newItem["Description"] = description;
    	newItem["Background Image Location"] = bgImage;
    	newItem["Link Location"] = linkLoc;
    	newItem["TileOrder"] = order;
    	newItem.Update();
    	list.Update();
    }
    
    private void CreateDummyLinks(SPWeb web)
    {
    	SPList list = SPListHelper.GetSPList(web, "My Links");
    	SPListItemCollection items = list.Items;
    	string contentType = "My Links";
    	
    	SPListHelper.AddPromotedLinkItem(list, contentType, "Announcements", "", web.Url + "/PublishingImages/announcements.png", web.Url + "/announcements",1);
    	SPListHelper.AddPromotedLinkItem(list, contentType, "News", "", web.Url + "/PublishingImages/news.png", web.Url + "/news",2);
    	SPListHelper.AddPromotedLinkItem(list, contentType, "Files", "", web.Url + "/PublishingImages/files.png", web.Url + "/files",3);
    
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by OCS.New Friday, April 21, 2017 12:55 PM
    Friday, April 21, 2017 8:24 AM
    Moderator
  • Can't believe it was that simple! Thanks a lot!
    Friday, April 21, 2017 12:55 PM