Thursday, February 23, 2012

Edit control block and site actions menu programmatically

Hi,


Here is how you can set edit control block on specific list and extend site actions menu by including a custom option in that.


This is equivalent to what we do with customactions tag via feature deployment.


This is simple button click example to achieve these two functionality


This is only for demonstration purpose. Write a logic in your own way.



     protected void btnAddCustomAction_Click(Object sender, EventArgs e)
        {
            SPWeb currentsite = SPContext.Current.Web;


            #region "Add custom action to a specific list"


            SPList lstOrders = currentsite.Lists.TryGetList("Orders");         


            if (lstOrders != null)
            {
                currentsite.AllowUnsafeUpdates = true;


                if (lstOrders.UserCustomActions.Count > 0)
                {
                    foreach (SPUserCustomAction action in lstOrders.UserCustomActions)
                    {
                        if (action.Name == "Google")
                        {
                            action.Delete();


                            lstOrders.Update();


                            break;
                        }
                    }
                }             
                        SPUserCustomAction customaction = lstOrders.UserCustomActions.Add();
                        customaction.Name = "Google";                
                        customaction.Location = "EditControlBlock";                
                        customaction.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";                        
                        customaction.Url = "https://www.google.com";
                        customaction.Sequence = 1000;
                        customaction.Title = "Google";                
                        customaction.Update();
                        lstOrders.Update();


                        currentsite.AllowUnsafeUpdates = false;
            }


            #endregion


            #region "Add custom action to a web"


            if (currentsite.UserCustomActions.Count > 0)
            {
                foreach (SPUserCustomAction action in currentsite.UserCustomActions)
                {
                    if (action.Name == "GoogleOnSiteAction")
                    {
                        currentsite.AllowUnsafeUpdates = true;


                        action.Delete();


                        currentsite.Update();


                        currentsite.AllowUnsafeUpdates = false;


                        break;
                    }
                }
            }


            currentsite.AllowUnsafeUpdates = true;


            SPUserCustomAction customactiononsiteactions = currentsite.UserCustomActions.Add();
            customactiononsiteactions.Name = "GoogleOnSiteAction";
            customactiononsiteactions.Location = "Microsoft.SharePoint.StandardMenu";
            customactiononsiteactions.Description = "Takes you to a google site";
            customactiononsiteactions.Group = "SiteActions";
            customactiononsiteactions.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";
            customactiononsiteactions.Url = "https://www.google.com";
            customactiononsiteactions.Sequence = 2000;
            customactiononsiteactions.Title = "Google";
            customactiononsiteactions.Update();
            currentsite.Update();


            currentsite.AllowUnsafeUpdates = false;


            #endregion


        }

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com