Search This Blog

Thursday, April 26, 2012

Import from AD to Sharepoint Group All Users

public static void Import(string username, string domain,string completeDomain)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, completeDomain);
Console.WriteLine(ctx.Name + " --> " + ctx.UserName + " --> " + ctx.ConnectedServer);
Console.ReadKey();

UserPrincipal findAllUser = new UserPrincipal(ctx);

PrincipalSearcher ps = new PrincipalSearcher(findAllUser);

try
{
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(GetUrl()))
{
using (SPWeb web = elevatedSite.OpenWeb())
{
foreach (Principal user in ps.FindAll())
{
web
.AllowUnsafeUpdates = true;
elevatedSite
.AllowUnsafeUpdates = true;

Console.WriteLine(user.Name + "-->" + user.DistinguishedName);
Console.ReadKey();

var loginName = domain + "\\" + user.SamAccountName;
var name = user.DisplayName;
web
.AssociatedVisitorGroup.Users.Add(loginName, string.Empty, name, string.Empty);

}
}

}
});
}
catch (Exception exx)
{
Console.WriteLine(exx.Message);
Console.WriteLine(exx.StackTrace);
Console.ReadKey();
}
Console.ReadLine();
}

Wednesday, April 25, 2012

Check if User Has Permissions on List or WebSite

if (oWeb.DoesUserHavePermissions(SPBasePermissions.ViewPages))
{
SPList oList = oWeb.Lists[ListName];

if (oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems))
{
// some smart code
}
}

------------------------------------

Code Block # 1

using (SPSite oSite = new SPSite(http://xxx))

{

using (SPWeb oWeb = oSite.RootWeb)

{

if(oWeb.DoesUserHavePermissions( oWeb.RoleDefinitions["Contribute"].BasePermissions))

//Do Something
else

//Do Something

}

}
----------------------

Code Block # 2

oWeb.EffectiveBasePermissions== oWeb.RoleDefinitions["Contribute"].BasePermissions

Saturday, April 21, 2012

SPCALENDARVIEW

private SPCalendarItemCollection GetCalendarItems(DataTable results)
{
SPCalendarItemCollection items = new SPCalendarItemCollection();
using (SPWeb web = SPContext.Current.Web)
{
foreach (DataRow row in results.Rows)
{
SPCalendarItem item = new SPCalendarItem();

item.Title = row["Location"] as string +" "+ row["Title"] as string + "br" + Convert.ToDateTime(row["EventDate"]).ToShortTimeString() + "\n" + Convert.ToDateTime(row["EndDate"]).ToShortTimeString();
item.StartDate = (System.DateTime)row["EventDate"];
item.EndDate = (System.DateTime)row["EndDate"];
item.hasEndDate = true;
item.IsAllDayEvent = true;
item.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);

items.Add(item);
}
}

return items;
}

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

MyCalendar.DataSource = GetCalendarItems();

DataBind();

}

}

private SPCalendarItemCollection GetCalendarItems()

{

SPCalendarItemCollection calItems = new SPCalendarItemCollection();

using (SPSite site = new SPSite("your web url"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.GetList("Lists/IWCalendar");

foreach (SPListItem it in list.Items)

{

SPCalendarItem calItem = new SPCalendarItem();

calItem.StartDate = DateTime.Parse(it["Start Time"].ToString());

calItem.EndDate = DateTime.Parse(it["End Time"].ToString());

calItem.hasEndDate = true;

calItem.Title = it.Title;

///else properties

calItems.Add(it);

}

}

}

return calItems;

}


protected void EventsCalendar2_PreRender(object sender, EventArgs e)
{

SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["DateEvent"];
SPListItemCollection collListItems = oList.Items;
EventsCalendar2.DataSource = GetCalendarItems(oList.Items.GetDataTable());
EventsCalendar2.DataBind();
}