Search This Blog

Wednesday, December 10, 2014

Create a subsite programatically and add SPGroups


SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite osite = new SPSite(siteCollectionUrl))
{
using (SPWeb oweb = osite.OpenWeb())
{
SPWeb site = null;
site = oweb.Webs.Add(Weburl, projectname, projectdescription, EnglishLocaleId, sitetemplate, true, false);
site.Navigation.UseShared = true;
site.Update();
isSitecreated = true;
if (isSitecreated)
{
site.AllowUnsafeUpdates = true;
site.SiteGroups.Add(site.Name + "-Owners", site.CurrentUser, site.CurrentUser, "");
site.SiteGroups.Add(site.Name + "-Visitors", site.CurrentUser, site.CurrentUser, "");
site.SiteGroups.Add(site.Name + "-Members", site.CurrentUser, site.CurrentUser, "");
SPGroup gOwners = site.SiteGroups[site.Name + "-Owners"];
SPRoleAssignment assignmentOwner = new SPRoleAssignment(gOwners);
assignmentOwner.RoleDefinitionBindings.Add(site.RoleDefinitions["Full Control"]);
site.RoleAssignments.Add(assignmentOwner);

SPGroup gVisitors = site.SiteGroups[site.Name + "-Visitors"];
SPRoleAssignment assignmentVisitors = new SPRoleAssignment(gVisitors);
assignmentVisitors.RoleDefinitionBindings.Add(site.ParentWeb.RoleDefinitions["Read"]);
site.RoleAssignments.Add(assignmentVisitors);
//Add users for visitors group
//gVisitors.AddUser();


SPRoleDefinition roleDefinition = site.ParentWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPGroup gMembers = site.SiteGroups[site.Name + "-Members"];
SPRoleAssignment assignmentMembers = new SPRoleAssignment(gMembers);
assignmentMembers.RoleDefinitionBindings.Add(roleDefinition);
site.RoleAssignments.Add(assignmentMembers);

 site.AllowUnsafeUpdates = false;
}
 else
{
  isSitecreated = false;
 }
}
}
});

 This will help you to create site and groups programatically

Bind values into dropdown list using SQL table.

Bind values into dropdown list using SQL table.


SqlCommand Bicmd;
SqlDataAdapter BiAdap;

DataTable BiDt;

 private void bindApplication()
        {
            try
            {
                SqlConnection con = new SqlConnection(connStr);
                con.Open();
                Bicmd = new SqlCommand("select Application as Name from WFE_ApplicationMaster", con);
                BiAdap = new SqlDataAdapter(Bicmd);
                BiDt = new DataTable();
                BiAdap.Fill(BiDt);
                ddlApplication.Items.Clear();
                ddlApplication.DataSource = BiDt;
                ddlApplication.DataBind();
                ddlApplication.DataTextField = "Name";
                //ddlApplication.DataValueField = "ID";
                ddlApplication.DataBind();
                ddlApplication.Items.Insert(0, "--Select--");
                BiAdap.Dispose();
            }
            catch (Exception)
            {
             
                throw;
            }

        }

This will help you to bind values from SQL table to dropdown list

Radio button list validation using JQuery.

Radio button list validation using JQuery.


<asp:RadioButtonList ID="rbIsactive" CssClass="radioButtonList" RepeatDirection="Horizontal" runat="server" ValidationGroup="g">
<asp:ListItem Value="0">Active</asp:ListItem>
<asp:ListItem Value="1">In Active</asp:ListItem>

</asp:RadioButtonList>

if ($("[id$=<%=rbIsactive.ClientID%>]").find("input:checked").length < 1) {
            var RB1 = document.getElementById("<%=rbIsactive.ClientID%>");
            var radio = RB1.getElementsByTagName("input");
            for (var i = 0; i < radio.length; i++) {
                if (radio[i].checked) {
                    break;
                }
                else if (i == radio.length - 1) {
                    rvalue = false;
                    alerts = "Please Select Workflow Status";
                    radio[0].focus();
                }
            }

        }


This will help you validate radio button list

Remove spgroups using powershell.

Remove spgroups using powershell.

When you delete a site spgroups for that site will remain in webapplication itself. So we have to remove that spgroups manually. Otherwise it will return "Group name already exist" exception when you create a spgroup with same/old name.

$spWeb = Get-SPWeb "Web Url"
$spGroups = $spWeb.SiteGroups

$groups = ("MID-10000-Owners", "MID-10002-Visitors","MID-10003-Members")

ForEach($group in $groups) {
   $spGroups.Remove($group)
}

$spWeb.Dispose()

This will help you to remove spgroups using shell command.

Know all field's internal name:

Know all field's internal name:

http://blogs.msdn.com/b/michael_yeager/archive/2008/11/03/reference-list-for-internal-field-names.aspx

Bind list items into a dropdown list using c#

Bind list items into a dropdown list using c#

if (!Page.IsPostBack)
                {
                    using (SPSite mySite = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb myWeb = mySite.OpenWeb())
                        {
                            SPList list = myWeb.Lists.TryGetList("Employee Info");
                            if (list != null)
                            {
                                SPQuery query = new SPQuery();
                                query.Query = @"<Where><Eq><FieldRef Name='FieldToCompare' LookupId='TRUE'/><Value Type='Integer' ><UserID/></Value></Eq></Where>";
                                query.ViewFields = "<FieldRef Name='Title'/>";
                                query.ViewFieldsOnly = true;
                                SPListItemCollection itemColl = list.GetItems(query);
                                if (itemColl != null)
                                {
                                    EmployeeDD.DataSource = itemColl;
                                    EmployeeDD.DataBind();
                                }
                            }
                        }
                    }

                }

This will help you to bind list items into a dropdown list

Open an application page using pop up window..

Open an application page using pop up window..

function AddTopics() {
        var options = {
            width: 500,
            height: 320,
            url: /applicationpage.aspx, //Here your application page url
            title: "Manage Meeting Topics"
        };
        SP.UI.ModalDialog.showModalDialog(options);
        return false;

    }

This will help you to work with pop up window

Restricting Custom People Picker to only one Sharepoint group programatically

Refer the following script files in your page,     <!-- For People Picker -->     <script type="text/javascript" src...