21 September 2011

How to Convert Image to Byte Array or Bitmap

This time I present code that can help you work with images (System.Drawing).
I showed how to convert Image to Byte Array and how to convert Image to Bitmap.

here is the code:

/// <summary>
/// Image Converter from Image to byte array
/// </summary>
/// <param name="img">Image</param>
/// <returns>byte</returns>
public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

/// <summary>
/// Image Converter from Image to Bitmap
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public static Bitmap ImageToBitmap(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (Bitmap)converter.ConvertTo(img, typeof(Bitmap));
}

Yours,
Roi

16 September 2011

Whether the current user belongs to a SharePoint group policy

This time I will present the work Code which provides the current user has identification.

    #region Permission
    /// <summary>
    /// return true is the CurrentUser has Permission in CurrentWeb
    /// groupNames is the groups that split by ';'.
    /// </summary>
    /// <param name="groupNames"></param>
    /// <returns></returns>
    public static bool IsUserHasPermission(string groupNames)
    {
      SPUser user = SPContext.Current.Web.CurrentUser;
      SPWeb web = SPContext.Current.Web;
      return IsUserHasPermission(groupNames,web,user);
    }
    /// <summary>
    /// return true is the User has Permission in Web
    /// groupNames is the groups that split by ';'.
    /// </summary>
    /// <param name="groupNames"></param>
    /// <returns></returns>
    public static bool IsUserHasPermission(string groupNames, SPWeb web,SPUser user)
    {
      try
      {
        string[] arrGroups = groupNames.Split(';');
        bool isUserHasPermission = false;
        if (!String.IsNullOrEmpty(groupNames))
        {
          if (user.Groups.OfType<SPGroup>().Where<SPGroup>(x => arrGroups.Contains(x.Name)).Count() > 0)
          {
            isUserHasPermission = true;
          }
          List<SPGroup> grps = new List<SPGroup>();
          // checking if current user exist in group
          foreach (SPGroup grp in web.Groups)
          {
            if (arrGroups.Contains(grp.Name))
              grps.Add(grp);
          }
          if (grps.Where<SPGroup>(x => x.ContainsCurrentUser).Count() > 0)
          {
            isUserHasPermission = true;
          }

        }
        return isUserHasPermission;
      }
      catch (Exception ex)
      {
        SetErrorMessage(ex);
        throw ex;
      }
    }
    #endregion

All you need to send method - This group names. You can also submit the web and user.

Example for groupNames :
string groupNames = "Group1;Group2;Group3";

yours,
Roi

11 September 2011

Error occurred in deployment step 'Recycle IIS Application Pool': The local SharePoint server is not available. Check that the server is running and connected to the SharePoint farm

Did you receive the following error when you tried to compile a project of SharePoint 2010?

Error occurred in deployment step 'Recycle IIS Application Pool': The local SharePoint server is not available. Check that the server is running and connected to the SharePoint farm.



So - the solution is simple
You should be Full Permission on the SharePoint Farm - including a data base (DB owner).
After it is completed - open the visual studio as administrator.

Cheers,
Roi Kolbinger

01 September 2011

Date and Time in JavaScript

And this time I will present a snippet of code in javascript that shows the date and time.


You can put it wherever you like - in nearly every segment SharePoint Designer or in the Content Editor WebPart.

The JS code shows Date and Time

<!-- Clock --><script language="JavaScript">
var
clockID = 0;
function
UpdateClock() {
  
if(clockID) {
     
clearTimeout(clockID);
     
clockID  = 0;
  
}
  
var tDate = new Date();
  
var roiClock = window.document.getElementById("roiClock");
  
roiClock.innerHTML = ""
   
+ tDate.toLocaleDateString()
   
+ "&nbsp;&nbsp;&nbsp;&nbsp;"
    
+ tDate.getHoursTwoDigits() + ":"
    
+ tDate.getMinutesTwoDigits()
  
clockID = setTimeout("UpdateClock()", 6000);
}

function
StartClock()
{

  
clockID = setTimeout("UpdateClock()", 500);
}

function
KillClock()
{

  
if(clockID) {
     
clearTimeout(clockID);
     
clockID  = 0;
  
}
}
StartClock();

// return Minutes with two digits (chars)

Date.prototype.getMinutesTwoDigits = function()
{
    
var retval = this.getMinutes();
    
if (retval < 10)
    
{
 
         return ("0" + retval.toString());
     
}
     else     {
        
return retval.toString();    
    
}
}

// return Hours with two digits (chars)

Date.prototype.getHoursTwoDigits = function()
{
     
var retval = this.getHours();
     
if (retval < 10)
     
{
         return ("0" + retval.toString());
     
}
     
else    
     
{
        
return retval.toString();
     
}
}

</
script>
<
div id="roiClock" style="color:#888;font-size:13pt;font-weight:bold;width:300px;" />
<!--- End Clock -->

Some code taken from this - http://www.htmlfreecodes.com/
Roi