Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Hi Just wanted to get some tips on SharePoint List created with the following code?

I created a helper method (UpdateDisplayName())to update the display name of the columns created is there anything wrong with passing the SPList by ref? I know this implies that you can alter the list.

But I thought it would be "faster" by reference since it's object type.

If anyone can suggest a better way of doing this, I would love to know?

Thanks.

   private void btnCreateProductList_Click(object sender, EventArgs e)
        {
            using (SPSite siteCollection = new SPSite(siteUrl))
            {
                SPWeb web = siteCollection.RootWeb;
                Guid listId = web.Lists.Add("Products", "A list of products", SPListTemplateType.GenericList);
                SPList list = web.Lists[listId];
                list.OnQuickLaunch = true;
                list.Update();


                // Modify display name of the Title field.
                SPField titleField = list.Fields.GetFieldByInternalName("Title");
                titleField.Title = "Category";
                titleField.Update();

                // add custom fields.
                var productDescription = list.Fields.Add("ProductDescription", SPFieldType.Note, false);
                var inventoryLevel = list.Fields.Add("InventoryLevel", SPFieldType.Integer, true);
                var listPrice = list.Fields.Add("ListPrice", SPFieldType.Currency, true);


                UpdateDisplayName(ref list, productDescription, "Product Description");
                UpdateDisplayName(ref list, inventoryLevel, "Inventory Level");
                UpdateDisplayName(ref list, listPrice, "List Price");

                SPView defaultView = list.DefaultView;
                defaultView.ViewFields.Add(productDescription);
                defaultView.ViewFields.Add(inventoryLevel);
                defaultView.ViewFields.Add(listPrice);
                defaultView.Update();

                lstResults.Items.Add("Products list created");
            }
        }

        private void UpdateDisplayName(ref SPList myList,string fieldName, string newName)
        {
            SPField field = myList.Fields.GetFieldByInternalName(fieldName);
            field.Title = newName;
            field.Update();
        }
share|improve this question
    
Looks ok to me. You could try running archive.msdn.microsoft.com/SPDisposeCheck to check for possible memleaks. As for performance, don't think that will matter much. But if you want to be sure; use counters to check things like execution time and memory usage. –  Jasper Jun 11 '12 at 15:23

1 Answer 1

I'd remove the ref if there is any difference in performance (which I doubt will be measurable) then the version with ref will be the slowest.

Whenever you pass a reference type (like SPList) to a function in .Net you pass it by reference. When you specify ref in front of it you're passing a reference to the reference to the function, the only reason to pass myList as a reference is if your function wants to be able to point the reference to the reference type to point to another object.

share|improve this answer
    
Thanks I wasn't aware SPList by default was passed by reference in .NET. –  TheLazyChap Jun 12 '12 at 2:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.