Monday, May 26, 2014

"object reference is not set to an instance" error on Feature Activation

Introduction

I am going to share some idea about resolving "object reference is not set to an instance" on Feature activate. On my case, I am having Feature event receiver with Some methods which is adding content to the pages. I was getting error with those method only. An "object reference is not set to an instance" has been cleared when i hidden those methods. Finally, I found the mistake where i did.

Solution

Just check the Feature scope and accessing Feature parent.

If your feature is having Site scope, 

 SPSite site = properties.Feature.Parent as SPSite

If your feature is having Web scope

 SPWeb web = properties.Feature.Parent as SPWeb;

At last, Check the AllowUnsafeUpdates before and after update.

In my case, I did the mistake in "AllowUnsafeUpdates" side. My issue has cleared once i set the UnsafeUpdates

Conclusion 

It is one of the solution to resolve the "object reference is not set to an instance" issue

Happy Coding.... !!!!

Wednesday, May 21, 2014

Connecting Two ListView Webpart Programmatically

Introduction

 I am going to share my knowledge about connecting two SharePoint webparts programmatically.

Lists

 First, we need to create a lists before going to connect them. We need to have an common field in both list to connect. We'll code the connection after creating lists.

Connection between WebParts

This is the code to connect the webparts,


private void ConnectWebpart()
        {
            var currentWeb = SPContext.Current.Web;
            var currentAllowUnsafeUpdatesValue = currentWeb.AllowUnsafeUpdates;
            
            try
            {
                currentWeb.AllowUnsafeUpdates = true;
                SPLimitedWebPartManager wpManager = SPContext.Current.Web.GetLimitedWebPartManager("PageURL", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                XsltListViewWebPart providerPart = null;
                XsltListViewWebPart consumerPart = null;


                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpManager.WebParts)
                {
                    if (wp.Title.Equals("ProviderTitle", StringComparison.CurrentCultureIgnoreCase))
                    {

                        providerPart = wp as XsltListViewWebPart;
                        providerPart.TitleUrl = "TitleURL";
            

                    }
                    else if (wp.Title.Equals("ConsumerTitle", StringComparison.CurrentCultureIgnoreCase))
                    {
                        consumerPart = wp as XsltListViewWebPart;
                        consumerPart.TitleUrl = "TitleURL";
                       
                       
                    }

                // get connectionpoints 
                System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection providerConnections = wpManager.GetProviderConnectionPoints(providerPart);
                System.Web.UI.WebControls.WebParts.ProviderConnectionPoint pCP = null;

                if (providerConnections != null)
                {
                    pCP = providerConnections["DFWP Row Provider ID"];
                }
                // Consumer webpart
                System.Web.UI.WebControls.WebParts.ConsumerConnectionPointCollection consumerConnections = wpManager.GetConsumerConnectionPoints(consumerPart);
                System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint cCP = null;
                if (consumerConnections != null)
                {
                    cCP = consumerConnections["DFWP Filter Consumer ID"];
                }

                SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();
                transformer.ProviderFieldNames = new string[] { "ProviderID" };
                transformer.ConsumerFieldNames = new string[] { "ConsumerID" };

                // connecting  Webpart
                SPWebPartConnection filterConn = wpManager.SPConnectWebParts(providerPart, pCP, consumerPart, cCP, transformer);
                if (!wpManager.SPWebPartConnections.Contains(filterConn))
                {
                    wpManager.SPWebPartConnections.Remove(filterConn);
                    wpManager.SPWebPartConnections.Add(filterConn);
                }

                
            }
            catch (Exception ex)
            {
                            }
            finally
            {
                currentWeb.AllowUnsafeUpdates = currentAllowUnsafeUpdatesValue;
            }
        }

Saturday, May 10, 2014

List View WebPart Filtering using Paramater Value

I am going to share about filtering list view webpart using querystring. We have an option to filter a list view using paramater. Here is the sample querystring for filter a list.

http://Server:Port/Sites/PageName.aspx?FilterField1=FieldName&FilterValue1=Value

The above code will work for if we have one list in the page. You need to mention the view GUID if you have more than one list. Here is the sample querystring to filter with view GUID

http://server:port/sites/Page.aspx#InplviewHashGUID=FilterField1%3DFieldName-FilterValue1%3DValue

Replace the italic text with your Values.

Happy Coding..... !!!!!!

Thursday, May 8, 2014

A field Update on drag and drop of Document Library using Event Receiver

A field Update on drag and drop of Document Library using Event Receiver


 Actually, i had a chance to update a field in document library using event receiver. I have used the synchronous method on event receiver to update an item in list. We need to write a code on ItemAdding method of event receiver. So we need to choose the "Item being added" in Event receiver type. Then do the code as below i mentioned.

 public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            if (properties.ListTitle == "Library Name")
            {
              

                    string strField = properties.List.Fields["FieldName"].InternalName;
                    properties.AfterProperties[strField] = "12346";
                    properties.Web.Update();
                   
            }
        }

That's all. Then deploy the solution and enjoy.

Happy Coding ... !!!!!