Home > development, Microsoft SharePoint 2010, SharePoint 2010 > Update a list view in sharepoint

Update a list view in sharepoint

Update a list view:

public static void UpdateListView(SPWeb web, string listName, string viewName, string fields, string query, int rowLimit, bool isDefault, string scope, bool isTabularView, bool allowInlineEditing)
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists.TryGetList(listName);

                if (list != null)
                {
                    SPView view = list.Views[viewName];

                    if (view != null)
                    {
                        SPViewFieldCollection fieldColl = view.ViewFields;

                        string[] flds = fields.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        fieldColl.DeleteAll();

                        foreach (string fld in flds)
                        {
                            fieldColl.Add(fld.Trim());
                        }

                        if (!string.IsNullOrEmpty(scope))
                        {
                            SPViewScope type = SPViewScope.Default;
                            foreach (var value in Enum.GetValues(typeof(SPViewScope)))
                            {
                                if (value.ToString() == scope)
                                {
                                    type = (SPViewScope)value;
                                    break;
                                }
                            }
                            view.Scope = type;
                        }

                        view.DefaultView = isDefault;
                        view.Query = query;
                        view.RowLimit = Convert.ToUInt32(rowLimit);
                        view.TabularView = isTabularView;
                        view.InlineEdit = allowInlineEditing.ToString();
                        view.Update();
                    }
                }
            }
            catch (Exception err)
            {
                Log.LogError(err.Message, err);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }

Call like this:

SPHelper.UpdateListView(myweb, “Your List Name”, “All Pictures”, “Title,Name,Description,Picture Size,Type”, string.Empty, 100, true, string.Empty, false, false);

“query” if you wish to filter items……
where viewName is an existing view……..

Hope this helps–
Rehman.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: