Skip to main content
added 12242 characters in body
Source Link
TZHX
  • 1.1k
  • 7
  • 19

And again...

Copy and paste this code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Lars_Printz_Create_Sagsside.Sags_Info_Receiver
{
    /// <summary> 
    /// List Item Events 
    /// </summary> 
    public class Sags_Info_Receiver : SPItemEventReceiver
    {
        /// <summary> 
        /// An item was added. 
        /// </summary> 
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            if (properties.ListTitle == "Sags Info")
            {
                // Local 
                SPWeb createWebsite = null;

                //long startnumber = 1166; 
                long startnumber = 1566;

                using (SPSite site = new SPSite(properties.WebUrl))
                using (SPWeb oWebsite = site.OpenWeb("Sager"))
                {

                    SPWebTemplate spWebTemplate;
                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"];
                    }
                    else
                    {
                        spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"];
                    }

                    long websiteid;
                    if (properties.ListItem["LP_SagsID"] == null)
                    {
                        websiteid = long.Parse(properties.ListItemId.ToString()) + startnumber;
                    }
                    else
                    {
                        websiteid = long.Parse(properties.ListItem["LP_SagsID"].ToString());

                    }

                    try
                    {
                        createWebsite = oWebsite.Webs.Add(websiteid.ToString(), websiteid.ToString() + " " + properties.ListItem.Title, properties.ListItem.Title, 1030, spWebTemplate, true, false);
                        SPGroup owner = createWebsite.ParentWeb.AssociatedVisitorGroup;
                        createWebsite.SiteGroups.Add(websiteid.ToString() + " " + properties.ListItem.Title + "-Gæster", owner, null, "Gæstegruppen");


                        if (properties.WebUrl == "http://sharepointdev")
                        {
                            SPGroup spGroup = oWebsite.SiteGroups["SharePointdev-medlemmer"];
                        }
                        else
                        {
                            SPGroup spGroup = oWebsite.SiteGroups["Lars Printz-medlemmer"];
                        }
                        SPRoleAssignment roleAssignment = new SPRoleAssignment(owner);
                        SPRoleDefinition roleDefinition = oWebsite.RoleDefinitions.GetByType(SPRoleType.Contributor);
                        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                        createWebsite.Update();
                        if (!createWebsite.HasUniqueRoleAssignments)
                        {
                            createWebsite.BreakRoleInheritance(false); // Ensure we don't inherit permissions from parent 
                        }
                        createWebsite.RoleAssignments.Add(roleAssignment);
                        createWebsite.Update();

                    }
                    catch (Exception)
                    {

                        properties.ListItem["Title"] = "Fejl med at lave site";
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    finally
                    {
                        SPList l = createWebsite.Lists["SagsInfo"]; // Lars Printz 
                        SPListItem li = l.Items.Add();
                        li["Title"] = properties.ListItem.Title;
                        li["LP_Postal"] = properties.ListItem["LP_Postal"];
                        li["LP_Address"] = properties.ListItem["LP_Address"];

                        if (properties.ListItem["LP_Sagstype"] != null)
                        {
                            string sagstypelookup = properties.ListItem["LP_Sagstype"].ToString();
                            string[] sagstypelookupArray = sagstypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["LP_Sagstype"] = sagstypelookupArray[0].ToString();
                        }
                        if (properties.ListItem["LP_Budtype"] != null)
                        {
                            string budtypelookup = properties.ListItem["LP_Budtype"].ToString();
                            string[] budtypelookupArray = budtypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["LP_Budtype"] = budtypelookupArray[0].ToString();
                        }
                        if (properties.ListItem["SagsStatus"] != null)
                        {
                            string sagsstatuslookup = properties.ListItem["SagsStatus"].ToString();
                            string[] sagsstatuslookupArray = sagsstatuslookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["SagsStatus"] = sagsstatuslookupArray[0].ToString();

                        }

                        li["LP_SagsID"] = websiteid.ToString();
                        li["Ansvarlig"] = properties.ListItem["Ansvarlig"];
                        li.Update();

                    } // End Finally 


                    SPFieldUrlValue value = new SPFieldUrlValue();

                    value.Description = properties.ListItem.Title + " - " + websiteid.ToString();

                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        value.Url = "http://sharepointdev/sager/" + websiteid.ToString();
                    }
                    else
                    {
                        value.Url = "https://printz.sharepoint.com/Sager/" + websiteid.ToString();
                    }

                    properties.ListItem["LP_Sagslink"] = value;
                    properties.ListItem["LP_SagsID"] = websiteid.ToString();

                    try
                    {
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                } // End Openweb 


            } // End if ListTitle == "Sags Info" 

        } // End ItemAdded 

        /// <summary> 
        /// An item was updated 
        /// </summary> 
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            if (properties.ListTitle == "Sags Info")
            {

                string strUrl = properties.WebUrl;


                long websiteid = int.Parse(properties.ListItem["LP_SagsID"].ToString());
                using (SPSite site = new SPSite(strUrl))
                using (SPWeb oWebsite = site.OpenWeb("Sager/" + websiteid.ToString()))
                {

                    SPList newl = oWebsite.Lists["SagsInfo"]; // Lars Printz 
                    if (newl.ItemCount == 0)
                    {
                        SPListItem newli = newl.Items.Add();
                        newli["Title"] = "Ny Sagsinfo";
                        newli.Update();

                    }
                    SPList l = oWebsite.Lists["SagsInfo"]; // Lars Printz 
                    SPListItem li = l.Items[0];
                    li["Title"] = properties.ListItem.Title;
                    li["LP_Postal"] = properties.ListItem["LP_Postal"];
                    li["LP_Address"] = properties.ListItem["LP_Address"];

                    if (properties.ListItem["LP_Sagstype"] != null)
                    {
                        string sagstypelookup = properties.ListItem["LP_Sagstype"].ToString();
                        string[] sagstypelookupArray = sagstypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["LP_Sagstype"] = sagstypelookupArray[0].ToString();
                    }
                    if (properties.ListItem["LP_Budtype"] != null)
                    {
                        string budtypelookup = properties.ListItem["LP_Budtype"].ToString();
                        string[] budtypelookupArray = budtypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["LP_Budtype"] = budtypelookupArray[0].ToString();
                    }
                    if (properties.ListItem["SagsStatus"] != null)
                    {
                        string sagsstatuslookup = properties.ListItem["SagsStatus"].ToString();
                        string[] sagsstatuslookupArray = sagsstatuslookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["SagsStatus"] = sagsstatuslookupArray[0].ToString();

                    }

                    li["Ansvarlig"] = properties.ListItem["Ansvarlig"];
                    li.Update();



                    SPFieldUrlValue value = new SPFieldUrlValue();

                    value.Description = properties.ListItem.Title + " - " + websiteid.ToString();

                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        value.Url = "http://sharepointdev/sager/" + websiteid.ToString();
                    }
                    else
                    {
                        value.Url = "https://printz.sharepoint.com/Sager/" + websiteid.ToString();
                    }


                    properties.ListItem["LP_Sagslink"] = value;

                    try
                    {
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                } // End Openweb 


            } // End if ListTitle == "Sags Info" 

        }  // End ItemUpdated 

    }
}

The differences are:

  • as decribed above, not re-declaring the variable.
  • you didn't declare an spGroup variable, I've assumed you actually want to use the SPGroup object that you called owner.

The code compiles now.


And again...

Copy and paste this code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Lars_Printz_Create_Sagsside.Sags_Info_Receiver
{
    /// <summary> 
    /// List Item Events 
    /// </summary> 
    public class Sags_Info_Receiver : SPItemEventReceiver
    {
        /// <summary> 
        /// An item was added. 
        /// </summary> 
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            if (properties.ListTitle == "Sags Info")
            {
                // Local 
                SPWeb createWebsite = null;

                //long startnumber = 1166; 
                long startnumber = 1566;

                using (SPSite site = new SPSite(properties.WebUrl))
                using (SPWeb oWebsite = site.OpenWeb("Sager"))
                {

                    SPWebTemplate spWebTemplate;
                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"];
                    }
                    else
                    {
                        spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"];
                    }

                    long websiteid;
                    if (properties.ListItem["LP_SagsID"] == null)
                    {
                        websiteid = long.Parse(properties.ListItemId.ToString()) + startnumber;
                    }
                    else
                    {
                        websiteid = long.Parse(properties.ListItem["LP_SagsID"].ToString());

                    }

                    try
                    {
                        createWebsite = oWebsite.Webs.Add(websiteid.ToString(), websiteid.ToString() + " " + properties.ListItem.Title, properties.ListItem.Title, 1030, spWebTemplate, true, false);
                        SPGroup owner = createWebsite.ParentWeb.AssociatedVisitorGroup;
                        createWebsite.SiteGroups.Add(websiteid.ToString() + " " + properties.ListItem.Title + "-Gæster", owner, null, "Gæstegruppen");


                        if (properties.WebUrl == "http://sharepointdev")
                        {
                            SPGroup spGroup = oWebsite.SiteGroups["SharePointdev-medlemmer"];
                        }
                        else
                        {
                            SPGroup spGroup = oWebsite.SiteGroups["Lars Printz-medlemmer"];
                        }
                        SPRoleAssignment roleAssignment = new SPRoleAssignment(owner);
                        SPRoleDefinition roleDefinition = oWebsite.RoleDefinitions.GetByType(SPRoleType.Contributor);
                        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                        createWebsite.Update();
                        if (!createWebsite.HasUniqueRoleAssignments)
                        {
                            createWebsite.BreakRoleInheritance(false); // Ensure we don't inherit permissions from parent 
                        }
                        createWebsite.RoleAssignments.Add(roleAssignment);
                        createWebsite.Update();

                    }
                    catch (Exception)
                    {

                        properties.ListItem["Title"] = "Fejl med at lave site";
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    finally
                    {
                        SPList l = createWebsite.Lists["SagsInfo"]; // Lars Printz 
                        SPListItem li = l.Items.Add();
                        li["Title"] = properties.ListItem.Title;
                        li["LP_Postal"] = properties.ListItem["LP_Postal"];
                        li["LP_Address"] = properties.ListItem["LP_Address"];

                        if (properties.ListItem["LP_Sagstype"] != null)
                        {
                            string sagstypelookup = properties.ListItem["LP_Sagstype"].ToString();
                            string[] sagstypelookupArray = sagstypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["LP_Sagstype"] = sagstypelookupArray[0].ToString();
                        }
                        if (properties.ListItem["LP_Budtype"] != null)
                        {
                            string budtypelookup = properties.ListItem["LP_Budtype"].ToString();
                            string[] budtypelookupArray = budtypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["LP_Budtype"] = budtypelookupArray[0].ToString();
                        }
                        if (properties.ListItem["SagsStatus"] != null)
                        {
                            string sagsstatuslookup = properties.ListItem["SagsStatus"].ToString();
                            string[] sagsstatuslookupArray = sagsstatuslookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                            li["SagsStatus"] = sagsstatuslookupArray[0].ToString();

                        }

                        li["LP_SagsID"] = websiteid.ToString();
                        li["Ansvarlig"] = properties.ListItem["Ansvarlig"];
                        li.Update();

                    } // End Finally 


                    SPFieldUrlValue value = new SPFieldUrlValue();

                    value.Description = properties.ListItem.Title + " - " + websiteid.ToString();

                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        value.Url = "http://sharepointdev/sager/" + websiteid.ToString();
                    }
                    else
                    {
                        value.Url = "https://printz.sharepoint.com/Sager/" + websiteid.ToString();
                    }

                    properties.ListItem["LP_Sagslink"] = value;
                    properties.ListItem["LP_SagsID"] = websiteid.ToString();

                    try
                    {
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                } // End Openweb 


            } // End if ListTitle == "Sags Info" 

        } // End ItemAdded 

        /// <summary> 
        /// An item was updated 
        /// </summary> 
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            if (properties.ListTitle == "Sags Info")
            {

                string strUrl = properties.WebUrl;


                long websiteid = int.Parse(properties.ListItem["LP_SagsID"].ToString());
                using (SPSite site = new SPSite(strUrl))
                using (SPWeb oWebsite = site.OpenWeb("Sager/" + websiteid.ToString()))
                {

                    SPList newl = oWebsite.Lists["SagsInfo"]; // Lars Printz 
                    if (newl.ItemCount == 0)
                    {
                        SPListItem newli = newl.Items.Add();
                        newli["Title"] = "Ny Sagsinfo";
                        newli.Update();

                    }
                    SPList l = oWebsite.Lists["SagsInfo"]; // Lars Printz 
                    SPListItem li = l.Items[0];
                    li["Title"] = properties.ListItem.Title;
                    li["LP_Postal"] = properties.ListItem["LP_Postal"];
                    li["LP_Address"] = properties.ListItem["LP_Address"];

                    if (properties.ListItem["LP_Sagstype"] != null)
                    {
                        string sagstypelookup = properties.ListItem["LP_Sagstype"].ToString();
                        string[] sagstypelookupArray = sagstypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["LP_Sagstype"] = sagstypelookupArray[0].ToString();
                    }
                    if (properties.ListItem["LP_Budtype"] != null)
                    {
                        string budtypelookup = properties.ListItem["LP_Budtype"].ToString();
                        string[] budtypelookupArray = budtypelookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["LP_Budtype"] = budtypelookupArray[0].ToString();
                    }
                    if (properties.ListItem["SagsStatus"] != null)
                    {
                        string sagsstatuslookup = properties.ListItem["SagsStatus"].ToString();
                        string[] sagsstatuslookupArray = sagsstatuslookup.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                        li["SagsStatus"] = sagsstatuslookupArray[0].ToString();

                    }

                    li["Ansvarlig"] = properties.ListItem["Ansvarlig"];
                    li.Update();



                    SPFieldUrlValue value = new SPFieldUrlValue();

                    value.Description = properties.ListItem.Title + " - " + websiteid.ToString();

                    if (properties.WebUrl == "http://sharepointdev")
                    {
                        value.Url = "http://sharepointdev/sager/" + websiteid.ToString();
                    }
                    else
                    {
                        value.Url = "https://printz.sharepoint.com/Sager/" + websiteid.ToString();
                    }


                    properties.ListItem["LP_Sagslink"] = value;

                    try
                    {
                        this.EventFiringEnabled = false;
                        properties.ListItem.SystemUpdate(false);
                        this.EventFiringEnabled = true;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                } // End Openweb 


            } // End if ListTitle == "Sags Info" 

        }  // End ItemUpdated 

    }
}

The differences are:

  • as decribed above, not re-declaring the variable.
  • you didn't declare an spGroup variable, I've assumed you actually want to use the SPGroup object that you called owner.

The code compiles now.

added 689 characters in body
Source Link
TZHX
  • 1.1k
  • 7
  • 19

In the first section of the if, try removing spWebTemplateSPWebTemplate which seems to have got into your code, like below:

if (properties.WebUrl == "http://sharepointdev") 
{ 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
} 

Let's try again.

If you declare the variable inside the if statement, that's where it is scoped, so it will be removed once the code is passed. To avoid this, you can structure your code as follows:

SPWebTemplate spWebTemplate;
if (properties.WebUrl == "http://sharepointdev") 
{ 
     spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
} 

This is similar to what you tried before, but without re-declaring the variable; merely assigning a value to it.

In the first section of the if, try removing spWebTemplateSPWebTemplate which seems to have got into your code, like below:

if (properties.WebUrl == "http://sharepointdev") 
{ 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
} 

In the first section of the if, try removing spWebTemplateSPWebTemplate which seems to have got into your code, like below:

if (properties.WebUrl == "http://sharepointdev") 
{ 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
} 

Let's try again.

If you declare the variable inside the if statement, that's where it is scoped, so it will be removed once the code is passed. To avoid this, you can structure your code as follows:

SPWebTemplate spWebTemplate;
if (properties.WebUrl == "http://sharepointdev") 
{ 
     spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
} 

This is similar to what you tried before, but without re-declaring the variable; merely assigning a value to it.

Source Link
TZHX
  • 1.1k
  • 7
  • 19

In the first section of the if, try removing spWebTemplateSPWebTemplate which seems to have got into your code, like below:

if (properties.WebUrl == "http://sharepointdev") 
{ 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{378B494F-F38C-4F07-8D25-CA50E64C209F}#TemplateKSI"]; 
} else { 
     SPWebTemplate spWebTemplate = oWebsite.Site.GetWebTemplates(1030)["{5FC51B7F-355E-431F-9C65-3663590346F4}#Sag"]; 
}