Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

create a spview and set columns and set this as a default view

$
0
0

Hi,

I want to create a spview on a doc lib and i wanna set this as the default one programmatically. is there any way i can do this?

StringCollection  viewFields;
            SPView  newVendorView =null;


        string myviewname="Vendor Doc Lib View";
            SPSite site = null;
            string currsite = SPContext.Current.Web.Url.ToString();
            try
            {
                using (SPSite oosite = new SPSite(currsite))
                {
                    using (SPWeb ooWeb = oosite.OpenWeb())
                    {
                        ooWeb.AllowUnsafeUpdates = true;

                        SPList vendorList = ooWeb.Lists.TryGetList("Vendor_Lib");
                        // Add an extra view to the list

                        viewFields = new StringCollection();

                        viewFields.Add("Discipline");
                        viewFields.Add("StdDocNum");
                        viewFields.Add("Revision");
                        viewFields.Add("ApprovalCode");
                        viewFields.Add("ReasonforIssue");
                        viewFields.Add("ResponsibleOffice");
                        viewFields.Add("DocAuthor");
                        viewFields.Add("PONumber");
                        try
                        {
                            newVendorView = vendorList.Views[myviewname];
                            if (newVendorView == null)
                            {
                                newVendorView = vendorList.Views.Add(myviewname, viewFields, "", 100, true, true, SPViewCollection.SPViewType.Html, false);
                                newVendorView.ContentTypeId = new SPContentTypeId("0x012001");
                            }
                            else
                            {
                                newVendorView = vendorList.Views["Vendors View"];
                            }
                        }
                        catch (Exception)
                        {
                        }
                        newVendorView.ViewFields.Add("Discipline");
                        newVendorView.ViewFields.Add("StdDocNum");
                        newVendorView.ViewFields.Add("Revision");
                        newVendorView.ViewFields.Add("ApprovalCode");
                        newVendorView.ViewFields.Add("ReasonforIssue");
                        newVendorView.ViewFields.Add("ResponsibleOffice");
                        newVendorView.ViewFields.Add("DocAuthor");
                        newVendorView.ViewFields.Add("PONumber");
                        newVendorView.Update();
                        vendorList.Update();

I have written the above code. But my reqmnt is that,I need to set this when my doc lib is created[ provisioned ]. For testing purpose I have coded this on button click, it throws me error.  should i create this view on a spfeature recvr and  read this  spview on my web part? or  should i create the view on  my  web part's button click and proceed?                                                                                                           Can anyone pls help, where should i write this code[  am sure , it should not happen on button click ].





Viewing all articles
Browse latest Browse all 25064

Trending Articles