internal static ListBindContext LookupBindContext(ListItemModelHost listItemModelHost,
    XsltListViewWebPartDefinition wpModel)
 {
     return LookupBindContext(listItemModelHost,
         wpModel.WebUrl, wpModel.WebId,
         wpModel.ListUrl, wpModel.ListTitle, wpModel.ListId,
         wpModel.ViewName, wpModel.ViewId,
         wpModel.TitleUrl);
 }
        private ListBindContext LookupBindContext(ListItemModelHost listItemModelHost, XsltListViewWebPartDefinition wpModel)
        {
            var result = new ListBindContext
            {

            };

            var web = listItemModelHost.HostWeb;
            var context = listItemModelHost.HostWeb.Context;

            var list = LookupList(listItemModelHost, wpModel);

            View view = null;

            if (wpModel.ViewId.HasValue && wpModel.ViewId != default(Guid))
                view = list.Views.GetById(wpModel.ViewId.Value);
            else if (!string.IsNullOrEmpty(wpModel.ViewName))
                view = list.Views.GetByTitle(wpModel.ViewName);

            context.Load(list, l => l.Id);
            context.Load(list, l => l.DefaultViewUrl);
            context.Load(list, l => l.Title);
            context.Load(list, l => l.DefaultView);

            if (view != null)
            {
                context.Load(view);
                context.ExecuteQueryWithTrace();

                result.OriginalView = list.DefaultView;
                result.OriginalViewId = list.DefaultView.Id;

                result.TargetView = view;
                result.TargetViewId = view.Id;

                result.TitleUrl = view.ServerRelativeUrl;
            }
            else
            {
                context.ExecuteQueryWithTrace();
            }

            result.ListId = list.Id;

            if (wpModel.TitleUrl == null)
            {
                if (string.IsNullOrEmpty(result.TitleUrl))
                    result.TitleUrl = list.DefaultViewUrl;
            }

            return result;
        }
 private static List LookupList(ListItemModelHost listItemModelHost, XsltListViewWebPartDefinition wpModel)
 {
     return LookupList(listItemModelHost.HostWeb,
                 wpModel.ListUrl,
                 wpModel.ListTitle,
                 wpModel.ListId);
 }
 public static ModelNode AddXsltListViewWebPart(this ModelNode model, XsltListViewWebPartDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddXsltListViewWebPart(this ModelNode model, XsltListViewWebPartDefinition definition)
 {
     return AddXsltListViewWebPart(model, definition, null);
 }
        private List LookupList(ListItemModelHost listItemModelHost, XsltListViewWebPartDefinition wpModel)
        {
            var web = listItemModelHost.HostWeb;
            var context = listItemModelHost.HostWeb.Context;

            List list = null;

            if (!string.IsNullOrEmpty(wpModel.ListUrl))
            {
                list = web.QueryAndGetListByUrl(wpModel.ListUrl);
            }
            else if (!string.IsNullOrEmpty(wpModel.ListTitle))
            {
                list = web.Lists.GetByTitle(wpModel.ListTitle);
            }
            else if (wpModel.ListId != default(Guid))
            {
                list = web.Lists.GetById(wpModel.ListId.Value);
            }
            else
            {
                throw new SPMeta2Exception("ListUrl, ListTitle or ListId should be defined.");
            }

            return list;
        }
        private List LookupList(ListItemModelHost listItemModelHost, 
            XsltListViewWebPartDefinition wpModel
            , out Guid webId)
        {
            var web = listItemModelHost.HostWeb;
            var context = web.Context;

            if (wpModel.WebId.HasGuidValue() || !string.IsNullOrEmpty(wpModel.WebUrl))
            {
                web = new LookupFieldModelHandler()
                                .GetTargetWeb(listItemModelHost.HostClientContext.Site,
                                        wpModel.WebUrl, wpModel.WebId);

                webId = web.Id;
            }
            else
            {
                context.Load(web);
                context.Load(web, w => w.Id);

                context.ExecuteQueryWithTrace();

                webId = web.Id;
            }

            List list = null;

            if (!string.IsNullOrEmpty(wpModel.ListUrl))
            {
                list = web.QueryAndGetListByUrl(wpModel.ListUrl);
            }
            else if (!string.IsNullOrEmpty(wpModel.ListTitle))
            {
                list = web.Lists.GetByTitle(wpModel.ListTitle);
            }
            else if (wpModel.ListId != default(Guid))
            {
                list = web.Lists.GetById(wpModel.ListId.Value);
            }
            else
            {
                throw new SPMeta2Exception("ListUrl, ListTitle or ListId should be defined.");
            }

            return list;
        }
        private List LookupList(ListItemModelHost listItemModelHost, XsltListViewWebPartDefinition wpModel)
        {
            var webId = default(Guid);

            return LookupList(listItemModelHost, wpModel, out webId);
        }