I am making this if statement and inside i am setting this SPWebTemplate which is a type. I think it would work fine, but it says that it does not exist in the current context.
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"];
}
The name 'spWebTemplate' does not exist in the current context
How can that be?
And it does not help setting the variable before like
SPWebTemplate spWebTemplate;
if(...
It then says:
A local variable named 'spWebTemplate' cannot be declared in this scope because it would give a different meaning to 'spWebTemplate', which is already used in a 'parent or current' scope to denote something else.
but this example works fine:
long websiteid;
if (properties.ListItem["LP_SagsID"] == null)
{
websiteid = long.Parse(properties.ListItemId.ToString()) + startnumber;
} else {
websiteid = long.Parse(properties.ListItem["LP_SagsID"].ToString());
}
---------- 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"))
{
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"];
}
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(spGroup);
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
}
}
ERROR CODES:
Error 1 The name 'spWebTemplate' does not exist in the current context C:\Lars Printz\Lars Printz Create Sagsside\Lars Printz Create Sagsside\Sags Info Receiver\Sags Info Receiver.cs 58 169 Lars Printz Create Sagsside
Error 2 The name 'spGroup' does not exist in the current context C:\Lars Printz\Lars Printz Create Sagsside\Lars Printz Create Sagsside\Sags Info Receiver\Sags Info Receiver.cs 70 79 Lars Printz Create Sagsside