Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

An object not set to instance of an object

$
0
0

I am getting an exception 

An object not set to instance of an object....Where am i going wrong???

public override void ItemAdded(SPItemEventProperties properties)
       {
           try
           {
               SPListItem spItem = properties.ListItem;
               SPList doclib = spItem.ParentList as SPDocumentLibrary;
               SPWeb oWebURL = doclib.ParentWeb;
               // SPDocumentLibrary doclib = (SPDocumentLibrary)list;

               oWebURL.AllowUnsafeUpdates = true;

               SPFieldCollection spFields = spItem.Fields;
               foreach (SPField spField in spFields) //loop through fields resetting them
               {
                   if (spField.Type != SPFieldType.Computed &&
                   spField.Type != SPFieldType.Invalid && !spField.ReadOnlyField)
                   {
                       try
                       {
                           spItem[spField.InternalName] = spItem[spField.InternalName];
                       }
                       catch (Exception e)
                       {
                           throw e;
                       }
                   }
                   SPContentType spContentType = doclib.ContentTypes[(SPContentTypeId)spItem["ContentTypeId"]];
                   //try to update the item with the content type's template url which is the updated one

                   if (spContentType != null)
                   {
                       string strTemplateLink = spItem["TemplateUrl"].ToString();

                       string[] str = strTemplateLink.Split('/');

                       if (strTemplateLink.Contains("http") || strTemplateLink.Contains("https"))
                       {



                               spItem["TemplateUrl"] = "~sitecollection/FormServerTemplates/" + str[str.Length - 1].ToString();
                           }
                       }
                   }
                   spItem.SystemUpdate(false);

               oWebURL.Update();
               oWebURL.AllowUnsafeUpdates = false;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       /// <summary>
       /// An item was updated.
       /// </summary>
       public override void ItemUpdated(SPItemEventProperties properties)
       {
           try
           {
               SPListItem spItem = properties.ListItem;
               SPList doclib = spItem.ParentList as SPDocumentLibrary;
               SPWeb oWebURL = doclib.ParentWeb;
               // SPDocumentLibrary doclib = (SPDocumentLibrary)list;

               oWebURL.AllowUnsafeUpdates = true;

               SPFieldCollection spFields = spItem.Fields;
               foreach (SPField spField in spFields) //loop through fields resetting them
               {
                   if (spField.Type != SPFieldType.Computed &&
                   spField.Type != SPFieldType.Invalid && !spField.ReadOnlyField)
                   {
                       try
                       {
                           spItem[spField.InternalName] = spItem[spField.InternalName];
                       }
                       catch (Exception e)
                       {
                           throw e;
                       }
                   }
                   SPContentType spContentType = doclib.ContentTypes[(SPContentTypeId)spItem["ContentTypeId"]];
                   //try to update the item with the content type's template url which is the updated one

                   if (spContentType != null)
                   {
                       string strTemplateLink = spItem["TemplateUrl"].ToString();

                       string[] str = strTemplateLink.Split('/');

                       if (strTemplateLink.Contains("http") || strTemplateLink.Contains("https"))
                       {


                               spItem["TemplateUrl"] = "~sitecollection/FormServerTemplates/" + str[str.Length - 1].ToString();

                       }
                   }
                   spItem.SystemUpdate(false);
               }
               oWebURL.Update();
               oWebURL.AllowUnsafeUpdates = false;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }


    }
}

                                                                                                                                                                                   

Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>