Fill a content editor webparts ….

 
Some customers want to control all content in a list, and then read this list.
My list contains three columns:
  • Url: the realtive url of the page that contains the webpart.
  • HtmlContent: multiline filed that contains the html content.
  • Execute : boolean field, a check if we should update this page or not.
For I’ll do a query on this list to retrieve the pages that needed to be updated.
I also use resource class that contains all my values for my queries,fieldnames,listnames enz…
The code is pretty clear.
 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    try
    {
        if (properties.Feature.Parent is SPWeb)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                SPWeb web = (SPWeb)properties.Feature.Parent;

                if (GlobalProcedures.CheckList(ResourcesClass.Lists.Information.ListName))
                {
                    SPList _listInformation = web.Lists[ResourcesClass.Lists.Information.ListName];
                    SPQuery _query = new SPQuery { Query = ResourcesClass.Caml.Queries.RetrieveInformationExecute };
                    SPListItemCollection _listItemCollection = _listInformation.GetItems(_query);

                    foreach (SPListItem _infiormation in _listItemCollection)
                    {
                        string _url = _infiormation[ResourcesClass.Lists.Information.Fields.PageUrl].ToString();
                        string _htmlContent = _infiormation[ResourcesClass.Lists.Information.Fields.HtmlContent].ToString();

                        PublishingWeb _publishingWeb = PublishingWeb.GetPublishingWeb(web);
                        SPListItem _item = web.GetListItem(SPUrlUtility.CombineUrl(web.Url, _infiormation[ResourcesClass.Lists.Information.Fields.PageUrl].ToString()));

                        if (_item != null)
                        {
                            PublishingPage _publishingPage = PublishingPage.GetPublishingPage(_item);

                            if (_item.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                            {
                                _publishingPage.CheckOut();
                            }
                            else
                            {
                                _item.File.UndoCheckOut();
                                _publishingPage.CheckOut();
                            }

                            using (SPLimitedWebPartManager _limitedWebPartManager = _item.File.GetLimitedWebPartManager(PersonalizationScope.Shared))
                            {
                                if (_limitedWebPartManager != null)
                                {
                                    foreach (System.Web.UI.WebControls.WebParts.WebPart _tmpWebPart in _limitedWebPartManager.WebParts)
                                    {
                                        try
                                        {
                                            if (_tmpWebPart.GetType().ToString().Equals(typeof(Microsoft.SharePoint.WebPartPages.ContentEditorWebPart).ToString()))
                                            {
                                                ContentEditorWebPart _contentEditorWebPart = (ContentEditorWebPart)_tmpWebPart;

                                                XmlDocument _xmlDocument = new XmlDocument();
                                                XmlElement _xmlElement = _xmlDocument.CreateElement("MyElement");
                                                _xmlElement.InnerText = _htmlContent;
                                                _contentEditorWebPart.Content = _xmlElement;
                                                //_contentEditorWebPart.Content = _xmlElement;


                                                _limitedWebPartManager.SaveChanges(_contentEditorWebPart);
                                                _publishingPage.CheckIn(String.Empty);

                                                if (_publishingWeb.PagesList.EnableModeration)
                                                {
                                                    _item.File.Publish(String.Empty);
                                                    _item.File.Approve(String.Empty);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            GlobalProcedures.WriteError(ex, "FeatureActivated");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }
    catch (Exception ex)
    {
        GlobalProcedures.WriteError(ex, "FeatureActivated");
    }

}
 
Good luck !

Published: 11/28/2011 | 0  Comments | 0  Links to this post

Properties.ListItem … be aware

When we write feature receivers we would like to use the following statement:

SPListItem _item =properties.ListItem;

This could cause problems regarding which context you’ll beloading.
Therefore use the following statement:

SPListItem _item =SPUrlUtility.CombineUrl(web.Url, properties.ListItem.Url);

 

Cheers


Published: 11/17/2011 | 0  Comments | 0  Links to this post

File to byte[] and back ….

 

Sometimes you need to put a file into a stirng or so.
Or you’ll find situations where you need to convert a string towards a byte[] array
and then convert this one to a file.

this code will put a file which can be a picture , pdf whatever into a byte[], then save this byte[] as a string.
If you should send this ‘string’ to somebody else and he ‘decompilles’ it back, he would eventually get your file.

using (FileStream _fileStream = new FileStream(_picturePath, FileMode.Open, FileAccess.Read))
                {
                    byte[] _arr = new byte[_fileStream.Length];
                    _fileStream.Read(_arr, 0, Convert.ToInt32(_fileStream.Length));
                    _arrGlobal = _arr;
                    txtOuput.Text = Convert.ToBase64String(_arrGlobal, 0, _arrGlobal.Length);
                }
The other way arround
string _outputPath = "your path here";
              byte[] _arrTemp = Convert.FromBase64String(txtOuput.Text);

              using (FileStream _fileStream = new FileStream(_outputPath, FileMode.Create))
              {
                  _fileStream.Write(_arrTemp, 0, _arrTemp.Length);
              }
 
These is a basic technique, but on the net there are many ways of doing it.
This one worked for me.
 
Hope it helps !

Published: 11/4/2011 | 0  Comments | 0  Links to this post

Sending email using Gmail

private static string sendMail(System.Net.Mail.MailMessage mm)
      {
          try
          {
              string smtpHost = "smtp.gmail.com";
              string userName = "jefkepeeter@gmail.com";//write your email address
              string password = "************";//write password
              System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
              mClient.Port = 587;
              mClient.EnableSsl = true;
              mClient.UseDefaultCredentials = false;
              mClient.Credentials = new NetworkCredential(userName, password);
              mClient.Host = smtpHost;
              mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
              mClient.Send(mm);
          }
          catch (Exception ex)
          {
              System.Console.Write(ex.Message);
          }

          return "Send Sucessfully";
      }

Published: 11/4/2011 | 0  Comments | 0  Links to this post