Friday, July 20, 2012

Utilites Class for supporting SharePoint

Here is the code: Get File Size By

// Added by Young Ryu
public static string GetFileSizeBy(string fileSize)
{
    string[] sizes = { "B", "KB", "MB", "GB" };
    double len = Convert.ToDouble(fileSize);           
    int order = 0;
    while (len >= 1024 && order + 1 < sizes.Length)
    {
        order++;
        len = len / 1024;
    }
    string result = String.Format("{0:0.##} {1}", len, sizes[order]);
    return result;
}

Here is the code: SharePoint domain user contains in Active Directory.

// Added by Young Ryu       
public static bool IsUserContainedToAD(SPWeb myWeb, string user)
{
    try
    {
        SPUser _user = myWeb.EnsureUser(user);
        return true;
    }
    catch (SPException ex)
    {
        return false;
    }
}
Here is the code: Identify the user is FTE
       
//Added by Young Ryu
public static bool IsUserFTE(string strAuthor)
{
    string employeeID = strAuthor.Remove(0, 1);
    double Number;
    bool isNumber = double.TryParse(employeeID, out Number);
    if (isNumber)
        return true;
    else
        return false;
}

Here is the code: Group Exists in Web

//Added by Young Ryu
//Check if a group exists in a website
public static bool GroupExistsInWebSite(SPWeb web, string name)
{
    return web.Groups.OfType<SPGroup>().Count(g => g.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) > 0;
}

Here is the code: Group Exists in Site Collection

//Added by Young Ryu
//Check if a group exists in a site collection
public static bool GroupExistsInSiteCollection(SPWeb web, string name)
{
    return web.SiteGroups.OfType<SPGroup>().Count(g => g.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) > 0;
}

Here is the code: Get InternalFieldName programmatically


public static string GetFieldInternalFieldName(SPList list, string SPFieldDisplayName)
        {
            string internalName = string.Empty;
            for (int i = 0; i < list.Fields.Count; i++)
            {
                if (list.Fields[i].Title.Trim() == SPFieldDisplayName.Trim())
                {
                    internalName = list.Fields[i].InternalName;
                    break;
                }
            }
            return internalName;
        }

No comments:

Post a Comment